﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using System.IO;
using TTMain.UI;
using TTData;
using TTMain.Core;
using TTMain.Data;

namespace Thurn_und_Taxis
{
    /// <summary>
    /// Interaction logic for GameForm.xaml
    /// </summary>
    /// 
    public partial class GameForm : Window
    {

        #region Width and position constants
        // Constants with the default widths and heights of the visual elements on the layout 
        
        private static Int32 DEFAULT_HEIGHT = 1024;

        private static Int32 DEFAULT_HEIGHT_POSTAL_CARRIER = 148;
        private static Int32 DEFAULT_HEIGHT_POSTMASTER = 144;
        private static Int32 DEFAULT_HEIGHT_ADMINISTRATOR = 124;
        private static Int32 DEFAULT_HEIGHT_CART_WRIGHT = 145;
        
        private static Int32 DEFAULT_HEIGHT_IMAGE = 711;


        private static Int32 DEFAULT_HEIGHT_WRAP_HOUSES = 25;
        private static Int32 DEFAULT_HEIGHT_GRID_BIG_ROW = 730;
        private static Int32 DEFAULT_WIDTH_GRID_BIG_COLUMN = 1035;

        private static Int32 DEFAULT_HEIGHT_HOUSE = 25;
        private static Int32 DEFAULT_WIDTH_HOUSE = 25;
        
        private static Int32 DEFAULT_HEIGHT_CARD_BIG = 152;
        private static Int32 DEFAULT_HEIGHT_CARD_NORMAL = 116;

        private static Int32 DEFAULT_WIDTH_CARRIGE = 122;
        private static Int32 DEFAULT_HEIGHT_CARRIGE = 79;

        
        private static Int32 DEFAULT_HEIGHT_BONUS = 38;

        
        private static Int32 DEFAULT_HEIGHT_CITY_AUGSBURG = 96;
        private static Int32 DEFAULT_HEIGHT_CITY_BASEL = 86;
        private static Int32 DEFAULT_HEIGHT_CITY_BUDWEIS = 103;
        private static Int32 DEFAULT_HEIGHT_CITY_CARLSRUHE = 97;
        private static Int32 DEFAULT_HEIGHT_CITY_FREIBURG = 109;
        private static Int32 DEFAULT_HEIGHT_CITY_INGOLSTADT = 86;
        private static Int32 DEFAULT_HEIGHT_CITY_INNSBRUCK = 77;
        private static Int32 DEFAULT_HEIGHT_CITY_KEMPTEN = 91;
        private static Int32 DEFAULT_HEIGHT_CITY_LINZ = 98;
        private static Int32 DEFAULT_HEIGHT_CITY_LODZ = 84;
        private static Int32 DEFAULT_HEIGHT_CITY_MANNHEIM = 88;
        private static Int32 DEFAULT_HEIGHT_CITY_MUNCHEN = 102;
        private static Int32 DEFAULT_HEIGHT_CITY_NURENBERG = 102;
        private static Int32 DEFAULT_HEIGHT_CITY_PASSAU = 84;
        private static Int32 DEFAULT_HEIGHT_CITY_PILSEN = 93;
        private static Int32 DEFAULT_HEIGHT_CITY_REGENSBURG = 96;
        private static Int32 DEFAULT_HEIGHT_CITY_SALZBURG = 162;
        private static Int32 DEFAULT_HEIGHT_CITY_SIGMARINGEN = 104;
        private static Int32 DEFAULT_HEIGHT_CITY_STUTTGART = 90;
        private static Int32 DEFAULT_HEIGHT_CITY_ULM = 99;
        private static Int32 DEFAULT_HEIGHT_CITY_WURZBURG = 83;
        private static Int32 DEFAULT_HEIGHT_CITY_ZURICH = 110;

        private static Int32 DEFAULT_DISTANCE_TOP_CARD1 = 224;
        private static Int32 DEFAULT_DISTANCE_LEFT_CARD1 = 20;
        private static Int32 DEFAULT_GAP_CARD_HORIZONTAL = 88;
        private static Int32 DEFAULT_GAP_CARD_VERTICAL = 128;

        private static Int32 DEFAULT_DISTANCE_TOP_ADMINISTRATOR = 61;
        private static Int32 DEFAULT_DISTANCE_LEFT_ADMINISTRATOR = 136;

        private static Int32 DEFAULT_DISTANCE_TOP_POSTAL_CARRIER = 15;
        private static Int32 DEFAULT_DISTANCE_LEFT_POSTAL_CARRIER = 11;

        private static Int32 DEFAULT_DISTANCE_TOP_POST_MASTER = 21;
        private static Int32 DEFAULT_DISTANCE_LEFT_POST_MASTER = 72;

        private static Int32 DEFAULT_DISTANCE_TOP_CART_WRIGHT = 21;
        private static Int32 DEFAULT_DISTANCE_LEFT_CART_WRIGHT = 220;


        private static Int32 DEFAULT_DISTANCE_TOP_BONUS_BADEN = 122;
        private static Int32 DEFAULT_DISTANCE_LEFT_BONUS_BADEN = 426;

        private static Int32 DEFAULT_DISTANCE_TOP_BONUS_BAIERN = 386;
        private static Int32 DEFAULT_DISTANCE_LEFT_BONUS_BAIERN = 735;

        private static Int32 DEFAULT_DISTANCE_TOP_BONUS_SALZBURG = 379;
        private static Int32 DEFAULT_DISTANCE_LEFT_BONUS_SALZBURG = 904;

        private static Int32 DEFAULT_DISTANCE_TOP_BONUS_TYROL = 657;
        private static Int32 DEFAULT_DISTANCE_LEFT_BONUS_TYROL = 393;

        private static Int32 DEFAULT_DISTANCE_TOP_BONUS_WURTT = 364;
        private static Int32 DEFAULT_DISTANCE_LEFT_BONUS_WURTT = 376;

        private static Int32 DEFAULT_DISTANCE_TOP_BONUS_FINISH_GAME = 23;
        private static Int32 DEFAULT_DISTANCE_LEFT_BONUS_FINISH_GAME = 322;

        private static Int32 DEFAULT_DISTANCE_TOP_BONUS_ALL_REGIONS = 65;
        private static Int32 DEFAULT_DISTANCE_LEFT_BONUS_ALL_REGIONS = 324;

        private static Int32 DEFAULT_DISTANCE_TOP_BONUS_CARRIGE5 = 633;
        private static Int32 DEFAULT_DISTANCE_LEFT_BONUS_CARRIGE5 = 32;
        private static Int32 DEFAULT_GAP_BONUS_HORIZONTAL = 54;

        private static Int32 DEFAULT_DISTANCE_TOP_CARRIGE = 21;
        private static Int32 DEFAULT_DISTANCE_LEFT_CARRIGE = 868;

        private static Int32 DEFAULT_DISTANCE_TOP_CITY_AUGSBURG = 408;
        private static Int32 DEFAULT_DISTANCE_LEFT_CITY_AUGSBURG = 523;

        private static Int32 DEFAULT_DISTANCE_TOP_CITY_BASEL = 566;
        private static Int32 DEFAULT_DISTANCE_LEFT_CITY_BASEL = 200;

        private static Int32 DEFAULT_DISTANCE_TOP_CITY_BUDWEIS = 231;
        private static Int32 DEFAULT_DISTANCE_LEFT_CITY_BUDWEIS = 897;

        private static Int32 DEFAULT_DISTANCE_TOP_CITY_CARL = 276;
        private static Int32 DEFAULT_DISTANCE_LEFT_CITY_CARL = 220;

        private static Int32 DEFAULT_DISTANCE_TOP_CITY_FREI = 438;
        private static Int32 DEFAULT_DISTANCE_LEFT_CITY_FREI = 197;

        private static Int32 DEFAULT_DISTANCE_TOP_CITY_INGOL = 325;
        private static Int32 DEFAULT_DISTANCE_LEFT_CITY_INGOL = 590;

        private static Int32 DEFAULT_DISTANCE_TOP_CITY_INNS = 624;
        private static Int32 DEFAULT_DISTANCE_LEFT_CITY_INNS = 552;

        private static Int32 DEFAULT_DISTANCE_TOP_CITY_KEMPTEN = 531;
        private static Int32 DEFAULT_DISTANCE_LEFT_CITY_KEMPTEN = 447;

        private static Int32 DEFAULT_DISTANCE_TOP_CITY_LINZ = 412;
        private static Int32 DEFAULT_DISTANCE_LEFT_CITY_LINZ = 935;

        private static Int32 DEFAULT_DISTANCE_TOP_CITY_LODZ = 107;
        private static Int32 DEFAULT_DISTANCE_LEFT_CITY_LODZ = 906;

        private static Int32 DEFAULT_DISTANCE_TOP_CITY_MANN = 136;
        private static Int32 DEFAULT_DISTANCE_LEFT_CITY_MANN = 294;

        private static Int32 DEFAULT_DISTANCE_TOP_CITY_MUNCHEN = 459;
        private static Int32 DEFAULT_DISTANCE_LEFT_CITY_MUNCHEN = 644;

        private static Int32 DEFAULT_DISTANCE_TOP_CITY_NURENB = 165;
        private static Int32 DEFAULT_DISTANCE_LEFT_CITY_NURENB = 593;

        private static Int32 DEFAULT_DISTANCE_TOP_CITY_PASSAU = 420;
        private static Int32 DEFAULT_DISTANCE_LEFT_CITY_PASSAU = 786;

        private static Int32 DEFAULT_DISTANCE_TOP_CITY_PILSEN = 109;
        private static Int32 DEFAULT_DISTANCE_LEFT_CITY_PILSEN = 752;

        private static Int32 DEFAULT_DISTANCE_TOP_CITY_REGENS = 252;
        private static Int32 DEFAULT_DISTANCE_LEFT_CITY_REGENS = 709;

        private static Int32 DEFAULT_DISTANCE_TOP_CITY_SALZ = 518;
        private static Int32 DEFAULT_DISTANCE_LEFT_CITY_SALZ = 867;

        private static Int32 DEFAULT_DISTANCE_TOP_CITY_SIGM = 387;
        private static Int32 DEFAULT_DISTANCE_LEFT_CITY_SIGM = 322;

        private static Int32 DEFAULT_DISTANCE_TOP_CITY_STUTTGART = 235;
        private static Int32 DEFAULT_DISTANCE_LEFT_CITY_STUTTGART = 358;

        private static Int32 DEFAULT_DISTANCE_TOP_CITY_ULM = 370;
        private static Int32 DEFAULT_DISTANCE_LEFT_CITY_ULM = 415;

        private static Int32 DEFAULT_DISTANCE_TOP_CITY_WURZ = 105;
        private static Int32 DEFAULT_DISTANCE_LEFT_CITY_WURZ = 492;

        private static Int32 DEFAULT_DISTANCE_TOP_CITY_ZURICH = 585;
        private static Int32 DEFAULT_DISTANCE_LEFT_CITY_ZURICH = 304;

        private static Int32 DEFAULT_DISTANCE_TOP_WRAP_AUGSBURG = 455;
        private static Int32 DEFAULT_DISTANCE_LEFT_WRAP_AUGSBURG = 539;

        private static Int32 DEFAULT_DISTANCE_TOP_WRAP_BASEL = 603;
        private static Int32 DEFAULT_DISTANCE_LEFT_WRAP_BASEL = 204;

        private static Int32 DEFAULT_DISTANCE_TOP_WRAP_BUDWEIS = 287;
        private static Int32 DEFAULT_DISTANCE_LEFT_WRAP_BUDWEIS = 917;

        private static Int32 DEFAULT_DISTANCE_TOP_WRAP_CARL = 327;
        private static Int32 DEFAULT_DISTANCE_LEFT_WRAP_CARL = 232;

        private static Int32 DEFAULT_DISTANCE_TOP_WRAP_FREI = 500;
        private static Int32 DEFAULT_DISTANCE_LEFT_WRAP_FREI = 203;

        private static Int32 DEFAULT_DISTANCE_TOP_WRAP_INGOL = 364;
        private static Int32 DEFAULT_DISTANCE_LEFT_WRAP_INGOL = 593;

        private static Int32 DEFAULT_DISTANCE_TOP_WRAP_INNS = 653;
        private static Int32 DEFAULT_DISTANCE_LEFT_WRAP_INNS = 561;

        private static Int32 DEFAULT_DISTANCE_TOP_WRAP_KEMPTEN = 574;
        private static Int32 DEFAULT_DISTANCE_LEFT_WRAP_KEMPTEN = 461;

        private static Int32 DEFAULT_DISTANCE_TOP_WRAP_LINZ = 462;
        private static Int32 DEFAULT_DISTANCE_LEFT_WRAP_LINZ = 920;

        private static Int32 DEFAULT_DISTANCE_TOP_WRAP_LODZ = 140;
        private static Int32 DEFAULT_DISTANCE_LEFT_WRAP_LODZ = 913;

        private static Int32 DEFAULT_DISTANCE_TOP_WRAP_MANN = 177;
        private static Int32 DEFAULT_DISTANCE_LEFT_WRAP_MANN = 314;

        private static Int32 DEFAULT_DISTANCE_TOP_WRAP_MUNCHEN = 514;
        private static Int32 DEFAULT_DISTANCE_LEFT_WRAP_MUNCHEN = 658;

        private static Int32 DEFAULT_DISTANCE_TOP_WRAP_NURENB = 218;
        private static Int32 DEFAULT_DISTANCE_LEFT_WRAP_NURENB = 607;

        private static Int32 DEFAULT_DISTANCE_TOP_WRAP_PASSAU = 457;
        private static Int32 DEFAULT_DISTANCE_LEFT_WRAP_PASSAU = 799;

        private static Int32 DEFAULT_DISTANCE_TOP_WRAP_PILSEN = 158;
        private static Int32 DEFAULT_DISTANCE_LEFT_WRAP_PILSEN = 765;

        private static Int32 DEFAULT_DISTANCE_TOP_WRAP_REGENS = 300;
        private static Int32 DEFAULT_DISTANCE_LEFT_WRAP_REGENS = 715;

        private static Int32 DEFAULT_DISTANCE_TOP_WRAP_SALZ = 633;
        private static Int32 DEFAULT_DISTANCE_LEFT_WRAP_SALZ = 906;

        private static Int32 DEFAULT_DISTANCE_TOP_WRAP_SIGM = 438;
        private static Int32 DEFAULT_DISTANCE_LEFT_WRAP_SIGM = 314;

        private static Int32 DEFAULT_DISTANCE_TOP_WRAP_STUTTGART = 276;
        private static Int32 DEFAULT_DISTANCE_LEFT_WRAP_STUTTGART = 378;

        private static Int32 DEFAULT_DISTANCE_TOP_WRAP_ULM = 417;
        private static Int32 DEFAULT_DISTANCE_LEFT_WRAP_ULM = 428;

        private static Int32 DEFAULT_DISTANCE_TOP_WRAP_WURZ = 140;
        private static Int32 DEFAULT_DISTANCE_LEFT_WRAP_WURZ = 484;

        private static Int32 DEFAULT_DISTANCE_TOP_WRAP_ZURICH = 633;
        private static Int32 DEFAULT_DISTANCE_LEFT_WRAP_ZURICH = 316;



        #endregion
                
        private List<Button> _privateCardButtons, _publicCardButtons;
        private List<Player>  _playerList;
        private List<String> _temporaryHouses;
        private List<Image> _temporaryImageHouses;
        private List<Button> _temporaryButtons;
        private Int32 _roomId;
        private Boolean _isOwner;
        private GameUI _gameUI;

        private Int16 _statePrivateButtons;
        private Int16 _stateIndermediatePrivateButtons;
        private Button _privateButtonPressed, buttonLeft, buttonRight;

        private const Int16 STATE_PUT_DOWN = 0;
        private const Int16 STATE_DISCARD = 1;

        public GameForm(GameUI gameUI, Boolean isOwner, Int32 roomId)
        {
            if (gameUI != null)
            {
                /// Prepare visual elements
                InitializeComponent();
                Double ratio = System.Windows.SystemParameters.PrimaryScreenHeight / DEFAULT_HEIGHT;
                ResizeVisualComponents(ratio);
                AlignVisualComponents(ratio);

                /// Initial values
                _privateCardButtons = new List<Button>();
                _publicCardButtons = new List<Button>();                
                _privateButtonPressed = null;
                _gameUI = null;
                _isOwner = false;

                _stateIndermediatePrivateButtons = -2;
                _statePrivateButtons = -1;

                _roomId = roomId;
                _gameUI = gameUI;
                
                    
                _gameUI.SetRoomId(_roomId);
                _gameUI.UserJoinedTable += new UserJoinedTableHandler(_gameUI_UserJoinedTable);
                _gameUI.UserReady += new UserReadyHandler(_gameUI_UserReady);
                _gameUI.UserCancelReady += new UserCancelReadyHandler(_gameUI_UserCancelReady);
                _gameUI.GameStart += new GameStartHandler(_gameUI_GameStart);
                _gameUI.UserTurn += new UserTurnHandler(_gameUI_UserTurn);
                _gameUI.DrawCard += new DrawCardHandler(_gameUI_DrawCard);
                _gameUI.PutCard += new PutCardHandler(_gameUI_PutCard);
                _gameUI.ChangeCards += new ChangeCardsHandler(_gameUI_ChangeCards);
                _gameUI.PutHouses += new PutHousesHandler(_gameUI_PutHouses);
                _gameUI.DiscardPublicCards += new DiscardPublicCardsHandler(_gameUI_DiscardPublicCards);
                _gameUI.DiscardCards += new DiscardCardsHandler(_gameUI_DiscardCards);                
                _gameUI.Score += new ScoreHandler(_gameUI_Score);
                _gameUI.ScoreHelp += new ScoreHelpHandler(_gameUI_ScoreHelp);
                _gameUI.Quit += new QuitHandler(_gameUI_Quit);
                _gameUI.Disconnect += new DisconnectHandler(_gameUI_Disconnect);

                if (isOwner == true)
                {
                    _isOwner = true;
                    List<String> players = new List<String>();
                    List<Boolean> states = new List<Boolean>();
                    players.Add(null);
                    players.Add(null);
                    players.Add(null);
                    players.Add(null);

                    states.Add(false);
                    states.Add(false);
                    states.Add(false);
                    states.Add(false);

                    buttonReady.IsEnabled = false;
                    buttonReady.Content = "Start Game";                    
                    
                    players[0] = _gameUI.GetGameSettings().Nickname;                    
                    
                    ReceivedPlayerList(players, states);
                }                
            }
            else
            {
                this.Close();
            }
        }

        void _gameUI_ScoreHelp(object sender, TTData.TTEventArgs.NicknameEventArgs e)
        {
            try
            {
                this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                       (UpdateInterfaceCallback)delegate()
                       {
                           BonusCard bCard;

                           if (_gameUI != null)
                           {                               
                               CarrigeCard cCard = _gameUI.CalculateCarrigeCard(e.Nickname, true);
                               if (cCard != null)
                                   UpdateCarrigeCard(e.Nickname, cCard);
                           }

                       });
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }            
        }

        void _gameUI_Disconnect(object sender)
        {
            int i;
            i = 20;
            
        }

        void _gameUI_Quit(object sender, TTData.TTEventArgs.NicknameEventArgs e)
        {
            if (_gameUI != null)
            {
                _gameUI.UserDiscardPrivateCards(e.Nickname);
                _gameUI.UserDiscardPublicCards(e.Nickname);
            }

            try
            {

                this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                       (UpdateInterfaceCallback)delegate()
                       {
                           int playerIndex = GetPlayerDrawIndex(e.Nickname);

                           if (playerIndex == 1)
                           {                               
                               playerBox1.ClearPublicCards();
                               playerBox1.Opacity = 0.2;
                           }
                           else if (playerIndex == 2)
                           {
                               playerBox2.ClearPublicCards();
                               playerBox2.Opacity = 0.2;
                           }
                           else if (playerIndex == 3)
                           {
                               playerBox3.ClearPublicCards();
                               playerBox3.Opacity = 0.2;
                           }

                       });
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }

        void _gameUI_Score(object sender, TTData.TTEventArgs.NicknameEventArgs e)
        {
            try
            {

                this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                       (UpdateInterfaceCallback)delegate()
                       {
                           BonusCard bCard;

                           bCard = _gameUI.CalculateBonusAllRegion(e.Nickname);
                           if (bCard != null)
                               UpdateBonuses(e.Nickname, bCard);

                           bCard = _gameUI.CalculateBonusRegion(e.Nickname, BonusCard.BONUS_REGION_BADEN);
                           if (bCard != null)
                               UpdateBonuses(e.Nickname, bCard);

                           bCard = _gameUI.CalculateBonusRegion(e.Nickname, BonusCard.BONUS_REGION_BAIERN);
                           if (bCard != null)
                               UpdateBonuses(e.Nickname, bCard);

                           bCard = _gameUI.CalculateBonusRegion(e.Nickname, BonusCard.BONUS_REGION_BOHEM_SALZB);
                           if (bCard != null)
                               UpdateBonuses(e.Nickname, bCard);

                           bCard = _gameUI.CalculateBonusRegion(e.Nickname, BonusCard.BONUS_REGION_SCHWEIZ_TYROL);
                           if (bCard != null)
                               UpdateBonuses(e.Nickname, bCard);

                           bCard = _gameUI.CalculateBonusRegion(e.Nickname, BonusCard.BONUS_REGION_WURTT_HOHENZ);
                           if (bCard != null)
                               UpdateBonuses(e.Nickname, bCard);

                           /*****/
                           bCard = _gameUI.CalculateEndGame(e.Nickname);
                           if (bCard != null)
                           {
                               UpdateBonuses(e.Nickname, bCard);
                               EndGame();                               
                           }
                           /*****/

                       });
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }

        void _gameUI_DiscardCards(object sender, TTData.TTEventArgs.ListEventArgs e)
        {
            _gameUI.UserDiscardPrivateCards(e.Nickname, e.CityNameList);
        }

        void _gameUI_DiscardPublicCards(object sender, TTData.TTEventArgs.NicknameEventArgs e)
        {
            if (_gameUI != null)
                _gameUI.UserDiscardPublicCards(e.Nickname);

            try
            {

                this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                       (UpdateInterfaceCallback)delegate()
                       {
                           soundCardChange.Stop();
                           soundCardChange.Play();

                           int playerIndex = GetPlayerDrawIndex(e.Nickname);

                           if (playerIndex == 1)
                               playerBox1.ClearPublicCards();
                           else if (playerIndex == 2)
                               playerBox2.ClearPublicCards();
                           else if (playerIndex == 3)
                               playerBox3.ClearPublicCards();

                       });
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }

        void _gameUI_PutHouses(object sender, TTData.TTEventArgs.ListEventArgs e)
        {
            try
            {
                this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                       (UpdateInterfaceCallback)delegate()
                       {
                           BonusCard bCard;

                           if (_gameUI != null)
                           {
                               bCard = _gameUI.CalculateBonusRoadLength(e.Nickname);
                               if (bCard != null)
                                   UpdateBonuses(e.Nickname, bCard);

                               CarrigeCard cCard = _gameUI.CalculateCarrigeCard(e.Nickname, false);
                               if (cCard != null)
                                   UpdateCarrigeCard(e.Nickname, cCard);

                               _gameUI.UserPutHouses(e.Nickname, e.CityNameList);
                           }

                           String color = _gameUI.GetPlayerColor(e.Nickname);
                           for (int i = 0; i < e.CityNameList.Count; i++)
                               DrawHouseInCity(color, e.CityNameList[i]);

                           int playerIndex = GetPlayerDrawIndex(e.Nickname);

                           if (playerIndex == 1)
                           {
                               playerBox1.ClearPublicCards();
                               playerBox1.UpdateHouseCount(e.CityNameList.Count);
                           }
                           else if (playerIndex == 2)
                           {
                               playerBox2.ClearPublicCards();
                               playerBox2.UpdateHouseCount(e.CityNameList.Count);
                           }
                           else if (playerIndex == 3)
                           {
                               playerBox3.ClearPublicCards();
                               playerBox3.UpdateHouseCount(e.CityNameList.Count);
                           }

                       });
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
                      
        }

        void _gameUI_ChangeCards(object sender, TTData.TTEventArgs.ListEventArgs e)
        {
            if (_gameUI != null)
                _gameUI.UserChangedCards(e.CardList);

            this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                   (UpdateInterfaceCallback)delegate()
                   {
                       soundCardChange.Stop();
                       soundCardChange.Play();
                       Update6Cards(e.CardList);
                   });
        }

        void _gameUI_PutCard(object sender, TTData.TTEventArgs.CardEventArgs e)
        {
            String nickname = e.Nickname;
            Int16 position = (Int16)e.Position;
            String cardName = e.CityCardName;
            
            _gameUI.UserPutCard(nickname, cardName, position);

            this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                   (UpdateInterfaceCallback)delegate()
                   {
                       soundCardChange.Stop();
                       soundCardChange.Play();
                       int playerIndex = GetPlayerDrawIndex(nickname);
                       if (playerIndex == 1)
                           playerBox1.InsertPublicCard(cardName, position);
                       else if (playerIndex == 2)
                           playerBox2.InsertPublicCard(cardName, position);
                       else if (playerIndex == 3)
                           playerBox3.InsertPublicCard(cardName, position);
                   });
        }

        void _gameUI_DrawCard(object sender, TTData.TTEventArgs.CardEventArgs e)
        {
            if (e.Position == -1)
            {
                _gameUI.UserDrawCard(e.Nickname, e.CityCardName, e.Position);
            }
            else
            {
                this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                    (UpdateInterfaceCallback)delegate()
                    {
                        soundCardChange.Stop();
                        soundCardChange.Play();
                        UpdateCard(e.Position, e.CityCardName);
                        _gameUI.UserDrawCard(e.Nickname, e.CityCardName, e.Position);
                    });
            }            
        }

        void _gameUI_UserTurn(object sender, TTData.TTEventArgs.NicknameEventArgs e)
        {
            if ((_gameUI != null))
            {
                _gameUI.StartTurn(e.Nickname);

                int index = GetPlayerDrawIndex(e.Nickname);

                this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                    (UpdateInterfaceCallback)delegate()
                    {

                        switch (index)
                        {
                            case 0:
                                // enable all clickable items                                
                                RefreshMoveOptions(_gameUI.GetAvailableMoves(e.Nickname));
                                soundUserTurn.Stop();
                                soundUserTurn.Play();                                
                                playerBox1.Update2UserTurn(false);
                                playerBox2.Update2UserTurn(false);
                                playerBox3.Update2UserTurn(false);
                                break;
                            case 1:
                                // diable all clickable items
                                playerBox1.Update2UserTurn(true);
                                playerBox2.Update2UserTurn(false);
                                playerBox3.Update2UserTurn(false);
                                break;
                            case 2:
                                // diable all clickable items
                                playerBox2.Update2UserTurn(true);
                                playerBox1.Update2UserTurn(false);
                                playerBox3.Update2UserTurn(false);
                                break;
                            case 3:
                                // diable all clickable items
                                playerBox3.Update2UserTurn(true);
                                playerBox2.Update2UserTurn(false);
                                playerBox1.Update2UserTurn(false);
                                break;
                        }
                    });
            }
        }

        void _gameUI_GameStart(object sender, TTData.TTEventArgs.ListEventArgs e)
        {            
            int noPlayers = 0;
            List<Player> players = new List<Player>();            
            
            for (int i=0; i< _playerList.Count; i++) 
                if (_playerList[i] != null) {
                    noPlayers++;
                    players.Add(_playerList[i]);
                }

            if (e.CardList.Count == 6)
            {

                _gameUI.GameStarted(noPlayers, players, e.CardList);

                this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                    (UpdateInterfaceCallback)delegate()
                    {
                        Update6Cards(e.CardList);

                        buttonReady.Visibility = Visibility.Collapsed;                        
                        imageCarrige3.Visibility = Visibility.Visible;
                        imageCarrige4.Visibility = Visibility.Visible;
                        imageCarrige5.Visibility = Visibility.Visible;
                        imageCarrige6.Visibility = Visibility.Visible;
                        imageCarrige7.Visibility = Visibility.Visible;                        
                        imageBonusAllRegions.Visibility = Visibility.Visible;
                        imageBonusBaden.Visibility = Visibility.Visible;
                        imageBonusBaiern.Visibility = Visibility.Visible;
                        imageBonusFinish.Visibility = Visibility.Visible;
                        imageBonusCarrige5.Visibility = Visibility.Visible;
                        imageBonusCarrige6.Visibility = Visibility.Visible;
                        imageBonusCarrige7.Visibility = Visibility.Visible;
                        imageBonusSalzburg.Visibility = Visibility.Visible;
                        imageBonusTyrol.Visibility = Visibility.Visible;
                        imageBonusWurtt.Visibility = Visibility.Visible;
                        buttonDrawBackCard.Visibility = Visibility.Visible;

                        ResetAllMoves();
                    });
            }
            
        }

        void _gameUI_UserCancelReady(object sender, TTData.TTEventArgs.NicknameEventArgs e)
        {
            if (_gameUI != null)
            {
                Int32 index = GetPlayerDrawIndex(e.Nickname);
                Player p = GetPlayer(e.Nickname);
                p.IsReady = false;

                this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                    (UpdateInterfaceCallback)delegate()
                    {
                        switch (index)
                        {
                            case 1:
                                playerBox1.ChangeReadyState(false);
                                playerBox1.Opacity = 0.2;
                                break;
                            case 2:
                                playerBox2.ChangeReadyState(false);
                                playerBox2.Opacity = 0.2;
                                break;
                            case 3:
                                playerBox3.ChangeReadyState(false);
                                playerBox3.Opacity = 0.2;
                                break;
                        }
                        if (_isOwner)
                            UpdateStartButton();                        
                    }
                );
            }
        }

        void _gameUI_UserReady(object sender, TTData.TTEventArgs.NicknameEventArgs e)
        {
            if (_gameUI != null)
            {
                Int32 index = GetPlayerDrawIndex(e.Nickname);
                Player p = GetPlayer(e.Nickname);
                p.IsReady = true;
                
                this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                    (UpdateInterfaceCallback)delegate()
                    {
                        switch (index)
                        {
                            case 1:
                                playerBox1.Opacity = 1;
                                playerBox1.ChangeReadyState(true);
                                break;
                            case 2:
                                playerBox2.Opacity = 1;
                                playerBox2.ChangeReadyState(true);
                                break;
                            case 3:
                                playerBox3.Opacity = 1;
                                playerBox3.ChangeReadyState(true);
                                break;
                        }

                        if (_isOwner)
                            UpdateStartButton();

                    }
                );                
            }
        }

        void _gameUI_UserJoinedTable(object sender, TTData.TTEventArgs.TablePositionEventArgs e)
        {
            int position = e.Position;
            String nickname = e.Nickname;

            Player p = new Player(nickname, GetPlayerColor(position));
            int newIndex = e.Position;
            if (newIndex != -1)
                _playerList[newIndex] = p;            

            try
            {
                this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                    (UpdateInterfaceCallback)delegate()
                    {
                        RefreshView();
                        if (_isOwner)
                            UpdateStartButton();
                    }
                );
            }
            catch (Exception err)
            {

            }
            
        }        

        /// <summary>
        /// Resizes all the visual componenets of the game with the specified ratio
        /// </summary>
        /// <param name="ratio">the ratio value for resize</param>
        private void ResizeVisualComponents(Double ratio)
        {
            // Resize Grid Layout
            gridRowBig.Height   =   new GridLength(DEFAULT_HEIGHT_GRID_BIG_ROW * ratio);
            gridColumnBig.Width =   new GridLength(DEFAULT_WIDTH_GRID_BIG_COLUMN * ratio);

            // Resize map
            imageMap.Height = DEFAULT_HEIGHT_IMAGE * ratio;

            // Resize draw back Card
            imageDrawBackCard.Height = DEFAULT_HEIGHT_CARD_BIG * ratio;

            // Resize the 6 pack Cards
            imageCard1.Height = DEFAULT_HEIGHT_CARD_NORMAL * ratio;
            imageCard2.Height = DEFAULT_HEIGHT_CARD_NORMAL * ratio;
            imageCard3.Height = DEFAULT_HEIGHT_CARD_NORMAL * ratio;
            imageCard4.Height = DEFAULT_HEIGHT_CARD_NORMAL * ratio;
            imageCard5.Height = DEFAULT_HEIGHT_CARD_NORMAL * ratio;
            imageCard0.Height = DEFAULT_HEIGHT_CARD_NORMAL * ratio;


            // Resize bonuses
            imageBonusBaden.Height      = DEFAULT_HEIGHT_BONUS * ratio;
            imageBonusBaiern.Height     = DEFAULT_HEIGHT_BONUS * ratio;
            imageBonusSalzburg.Height   = DEFAULT_HEIGHT_BONUS * ratio;
            imageBonusTyrol.Height      = DEFAULT_HEIGHT_BONUS * ratio;
            imageBonusWurtt.Height      = DEFAULT_HEIGHT_BONUS * ratio;
            imageBonusFinish.Height     = DEFAULT_HEIGHT_BONUS * ratio;
            imageBonusAllRegions.Height = DEFAULT_HEIGHT_BONUS * ratio;
            imageBonusCarrige5.Height   = DEFAULT_HEIGHT_BONUS * ratio;
            imageBonusCarrige6.Height   = DEFAULT_HEIGHT_BONUS * ratio;
            imageBonusCarrige7.Height   = DEFAULT_HEIGHT_BONUS * ratio;

            // Resize carrige Cards
            imageCarrige3.Height = DEFAULT_HEIGHT_CARRIGE * ratio;
            imageCarrige4.Height = DEFAULT_HEIGHT_CARRIGE * ratio;
            imageCarrige5.Height = DEFAULT_HEIGHT_CARRIGE * ratio;
            imageCarrige6.Height = DEFAULT_HEIGHT_CARRIGE * ratio;            
            imageCarrige7.Height = DEFAULT_HEIGHT_CARRIGE * ratio;
            imageCarrige.Height = DEFAULT_HEIGHT_CARRIGE * ratio;

            // Resize helpers 
            imageAdministrator.Height = DEFAULT_HEIGHT_ADMINISTRATOR * ratio;
            imagePostalCarrier.Height = DEFAULT_HEIGHT_POSTAL_CARRIER * ratio;
            imagePostMaster.Height = DEFAULT_HEIGHT_POSTMASTER * ratio;
            imageCartWright.Height = DEFAULT_HEIGHT_CART_WRIGHT * ratio;

            buttonCityAugsburg.Height = DEFAULT_HEIGHT_CITY_AUGSBURG * ratio;
            buttonCityBasel.Height = DEFAULT_HEIGHT_CITY_BASEL * ratio;
            buttonCityBudweis.Height = DEFAULT_HEIGHT_CITY_BUDWEIS * ratio;
            buttonCityCarlsruhe.Height = DEFAULT_HEIGHT_CITY_CARLSRUHE * ratio;
            buttonCityFreiburg.Height = DEFAULT_HEIGHT_CITY_FREIBURG * ratio;
            buttonCityIngolstadt.Height = DEFAULT_HEIGHT_CITY_INGOLSTADT * ratio;
            buttonCityInnsbruck.Height = DEFAULT_HEIGHT_CITY_INNSBRUCK * ratio;
            buttonCityKempten.Height = DEFAULT_HEIGHT_CITY_KEMPTEN * ratio;
            buttonCityLinz.Height = DEFAULT_HEIGHT_CITY_LINZ * ratio;
            buttonCityLodz.Height = DEFAULT_HEIGHT_CITY_LODZ * ratio;
            buttonCityMannheim.Height = DEFAULT_HEIGHT_CITY_MANNHEIM * ratio;
            buttonCityMunchen.Height = DEFAULT_HEIGHT_CITY_MUNCHEN * ratio;
            buttonCityNurenberg.Height = DEFAULT_HEIGHT_CITY_NURENBERG * ratio;
            buttonCityPassau.Height = DEFAULT_HEIGHT_CITY_PASSAU * ratio;
            buttonCityPilsen.Height = DEFAULT_HEIGHT_CITY_PILSEN * ratio;
            buttonCityRegensburg.Height = DEFAULT_HEIGHT_CITY_REGENSBURG * ratio;
            buttonCitySalzburg.Height = DEFAULT_HEIGHT_CITY_SALZBURG * ratio;
            buttonCitySigmaringen.Height = DEFAULT_HEIGHT_CITY_SIGMARINGEN * ratio;
            buttonCityStuttgart.Height = DEFAULT_HEIGHT_CITY_STUTTGART * ratio;
            buttonCityUlm.Height = DEFAULT_HEIGHT_CITY_ULM * ratio;
            buttonCityWurzburg.Height = DEFAULT_HEIGHT_CITY_WURZBURG * ratio;
            buttonCityZurich.Height = DEFAULT_HEIGHT_CITY_ZURICH * ratio;
            

            wrapHousesAugsburg.Height = DEFAULT_HEIGHT_WRAP_HOUSES * ratio;
            wrapHousesBasel.Height = DEFAULT_HEIGHT_WRAP_HOUSES * ratio;
            wrapHousesBudweis.Height = DEFAULT_HEIGHT_WRAP_HOUSES * ratio;
            wrapHousesCarlsruhe.Height = DEFAULT_HEIGHT_WRAP_HOUSES * ratio;
            wrapHousesFreiburg.Height = DEFAULT_HEIGHT_WRAP_HOUSES * ratio;
            wrapHousesIngolstadt.Height = DEFAULT_HEIGHT_WRAP_HOUSES * ratio;
            wrapHousesInnsbruck.Height = DEFAULT_HEIGHT_WRAP_HOUSES * ratio;
            wrapHousesKempten.Height = DEFAULT_HEIGHT_WRAP_HOUSES * ratio;
            wrapHousesLinz.Height = DEFAULT_HEIGHT_WRAP_HOUSES * ratio;
            wrapHousesLodz.Height = DEFAULT_HEIGHT_WRAP_HOUSES * ratio;
            wrapHousesMannheim.Height = DEFAULT_HEIGHT_WRAP_HOUSES * ratio;
            wrapHousesMunchen.Height = DEFAULT_HEIGHT_WRAP_HOUSES * ratio;
            wrapHousesNurenberg.Height = DEFAULT_HEIGHT_WRAP_HOUSES * ratio;
            wrapHousesPassau.Height = DEFAULT_HEIGHT_WRAP_HOUSES * ratio;
            wrapHousesPilsen.Height = DEFAULT_HEIGHT_WRAP_HOUSES * ratio;
            wrapHousesRegensburg.Height = DEFAULT_HEIGHT_WRAP_HOUSES * ratio;
            wrapHousesSigmaringen.Height = DEFAULT_HEIGHT_WRAP_HOUSES * ratio;
            wrapHousesStuttgart.Height = DEFAULT_HEIGHT_WRAP_HOUSES * ratio;
            wrapHousesUlm.Height = DEFAULT_HEIGHT_WRAP_HOUSES * ratio;
            wrapHousesWurzburg.Height = DEFAULT_HEIGHT_WRAP_HOUSES * ratio;
            wrapHousesZurich.Height = DEFAULT_HEIGHT_WRAP_HOUSES * ratio;
            wrapHousesSalzburg.Height = DEFAULT_HEIGHT_WRAP_HOUSES * ratio;

            
        }

        /// <summary>
        /// Aligns all the visual componenets of the game with the specified ratio
        /// </summary>
        /// <param name="ratio">the ratio value for align</param>
        private void AlignVisualComponents(Double ratio)
        {
            // Align the 6 pack Cards
            buttonCard0.SetValue(Canvas.TopProperty,    DEFAULT_DISTANCE_TOP_CARD1  * ratio);
            buttonCard0.SetValue(Canvas.LeftProperty,   DEFAULT_DISTANCE_LEFT_CARD1 * ratio);

            buttonCard1.SetValue(Canvas.TopProperty,    DEFAULT_DISTANCE_TOP_CARD1 * ratio);
            buttonCard1.SetValue(Canvas.LeftProperty,   (DEFAULT_DISTANCE_LEFT_CARD1 + DEFAULT_GAP_CARD_HORIZONTAL) * ratio);

            buttonCard2.SetValue(Canvas.TopProperty,    (DEFAULT_DISTANCE_TOP_CARD1 + DEFAULT_GAP_CARD_VERTICAL) * ratio);
            buttonCard2.SetValue(Canvas.LeftProperty,   DEFAULT_DISTANCE_LEFT_CARD1 * ratio);

            buttonCard3.SetValue(Canvas.TopProperty,    (DEFAULT_DISTANCE_TOP_CARD1 + DEFAULT_GAP_CARD_VERTICAL) * ratio);
            buttonCard3.SetValue(Canvas.LeftProperty,   (DEFAULT_DISTANCE_LEFT_CARD1 + DEFAULT_GAP_CARD_HORIZONTAL) * ratio);

            buttonCard4.SetValue(Canvas.TopProperty,    (DEFAULT_DISTANCE_TOP_CARD1 + 2 * DEFAULT_GAP_CARD_VERTICAL) * ratio);
            buttonCard4.SetValue(Canvas.LeftProperty,   DEFAULT_DISTANCE_LEFT_CARD1 * ratio);

            buttonCard5.SetValue(Canvas.TopProperty,    (DEFAULT_DISTANCE_TOP_CARD1 + 2 * DEFAULT_GAP_CARD_VERTICAL) * ratio);
            buttonCard5.SetValue(Canvas.LeftProperty,   (DEFAULT_DISTANCE_LEFT_CARD1 + DEFAULT_GAP_CARD_HORIZONTAL) * ratio);

            // Align Helpers
            imageAdministrator.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_ADMINISTRATOR * ratio);
            imageAdministrator.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_ADMINISTRATOR * ratio);

            imagePostMaster.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_POST_MASTER * ratio);
            imagePostMaster.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_POST_MASTER * ratio);

            imagePostalCarrier.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_POSTAL_CARRIER * ratio);
            imagePostalCarrier.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_POSTAL_CARRIER * ratio);

            imageCartWright.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_CART_WRIGHT * ratio);
            imageCartWright.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_CART_WRIGHT * ratio);

            // Align Bonuses            
            imageBonusBaden.SetValue(Canvas.TopProperty,        DEFAULT_DISTANCE_TOP_BONUS_BADEN * ratio);
            imageBonusBaden.SetValue(Canvas.LeftProperty,       DEFAULT_DISTANCE_LEFT_BONUS_BADEN * ratio);

            imageBonusBaiern.SetValue(Canvas.TopProperty,       DEFAULT_DISTANCE_TOP_BONUS_BAIERN * ratio);
            imageBonusBaiern.SetValue(Canvas.LeftProperty,      DEFAULT_DISTANCE_LEFT_BONUS_BAIERN * ratio);

            imageBonusSalzburg.SetValue(Canvas.TopProperty,     DEFAULT_DISTANCE_TOP_BONUS_SALZBURG * ratio);
            imageBonusSalzburg.SetValue(Canvas.LeftProperty,    DEFAULT_DISTANCE_LEFT_BONUS_SALZBURG * ratio);

            imageBonusTyrol.SetValue(Canvas.TopProperty,        DEFAULT_DISTANCE_TOP_BONUS_TYROL * ratio);
            imageBonusTyrol.SetValue(Canvas.LeftProperty,       DEFAULT_DISTANCE_LEFT_BONUS_TYROL * ratio);

            imageBonusWurtt.SetValue(Canvas.TopProperty,        DEFAULT_DISTANCE_TOP_BONUS_WURTT * ratio);
            imageBonusWurtt.SetValue(Canvas.LeftProperty,       DEFAULT_DISTANCE_LEFT_BONUS_WURTT * ratio);

            imageBonusFinish.SetValue(Canvas.TopProperty,       DEFAULT_DISTANCE_TOP_BONUS_FINISH_GAME * ratio);
            imageBonusFinish.SetValue(Canvas.LeftProperty,      DEFAULT_DISTANCE_LEFT_BONUS_FINISH_GAME * ratio);

            imageBonusAllRegions.SetValue(Canvas.TopProperty,   DEFAULT_DISTANCE_TOP_BONUS_ALL_REGIONS * ratio);
            imageBonusAllRegions.SetValue(Canvas.LeftProperty,  DEFAULT_DISTANCE_LEFT_BONUS_ALL_REGIONS * ratio);

            imageBonusCarrige5.SetValue(Canvas.TopProperty,     DEFAULT_DISTANCE_TOP_BONUS_CARRIGE5 * ratio);
            imageBonusCarrige5.SetValue(Canvas.LeftProperty,    DEFAULT_DISTANCE_LEFT_BONUS_CARRIGE5 * ratio);

            imageBonusCarrige6.SetValue(Canvas.TopProperty,     DEFAULT_DISTANCE_TOP_BONUS_CARRIGE5 * ratio);
            imageBonusCarrige6.SetValue(Canvas.LeftProperty,    (DEFAULT_DISTANCE_LEFT_BONUS_CARRIGE5 + DEFAULT_GAP_BONUS_HORIZONTAL) * ratio);

            imageBonusCarrige7.SetValue(Canvas.TopProperty,     DEFAULT_DISTANCE_TOP_BONUS_CARRIGE5 * ratio);
            imageBonusCarrige7.SetValue(Canvas.LeftProperty,    (DEFAULT_DISTANCE_LEFT_BONUS_CARRIGE5 + 2 * DEFAULT_GAP_BONUS_HORIZONTAL) * ratio);

            // Align carrige Cards
            imageCarrige3.SetValue(Canvas.TopProperty,  DEFAULT_DISTANCE_TOP_CARRIGE * ratio);
            imageCarrige3.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_CARRIGE * ratio);

            imageCarrige4.SetValue(Canvas.TopProperty,  DEFAULT_DISTANCE_TOP_CARRIGE * ratio);
            imageCarrige4.SetValue(Canvas.LeftProperty, (DEFAULT_DISTANCE_LEFT_CARRIGE - DEFAULT_WIDTH_CARRIGE) * ratio);

            imageCarrige5.SetValue(Canvas.TopProperty,  DEFAULT_DISTANCE_TOP_CARRIGE * ratio);
            imageCarrige5.SetValue(Canvas.LeftProperty, (DEFAULT_DISTANCE_LEFT_CARRIGE - 2 * DEFAULT_WIDTH_CARRIGE) * ratio);

            imageCarrige6.SetValue(Canvas.TopProperty,  DEFAULT_DISTANCE_TOP_CARRIGE * ratio);
            imageCarrige6.SetValue(Canvas.LeftProperty, (DEFAULT_DISTANCE_LEFT_CARRIGE - 3 * DEFAULT_WIDTH_CARRIGE) * ratio);

            imageCarrige7.SetValue(Canvas.TopProperty,  DEFAULT_DISTANCE_TOP_CARRIGE * ratio);
            imageCarrige7.SetValue(Canvas.LeftProperty, (DEFAULT_DISTANCE_LEFT_CARRIGE - 4 * DEFAULT_WIDTH_CARRIGE) * ratio);

            buttonCityAugsburg.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_CITY_AUGSBURG * ratio);
            buttonCityAugsburg.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_CITY_AUGSBURG * ratio);

            buttonCityBasel.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_CITY_BASEL * ratio);
            buttonCityBasel.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_CITY_BASEL * ratio);

            buttonCityBudweis.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_CITY_BUDWEIS * ratio);
            buttonCityBudweis.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_CITY_BUDWEIS * ratio);

            buttonCityCarlsruhe.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_CITY_CARL * ratio);
            buttonCityCarlsruhe.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_CITY_CARL * ratio);

            buttonCityFreiburg.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_CITY_FREI * ratio);
            buttonCityFreiburg.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_CITY_FREI * ratio);

            buttonCityIngolstadt.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_CITY_INGOL * ratio);
            buttonCityIngolstadt.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_CITY_INGOL * ratio);

            buttonCityInnsbruck.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_CITY_INNS * ratio);
            buttonCityInnsbruck.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_CITY_INNS * ratio);

            buttonCityKempten.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_CITY_KEMPTEN * ratio);
            buttonCityKempten.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_CITY_KEMPTEN * ratio);

            buttonCityLinz.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_CITY_LINZ * ratio);
            buttonCityLinz.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_CITY_LINZ * ratio);

            buttonCityLodz.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_CITY_LODZ * ratio);
            buttonCityLodz.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_CITY_LODZ * ratio);

            buttonCityMannheim.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_CITY_MANN * ratio);
            buttonCityMannheim.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_CITY_MANN * ratio);

            buttonCityMunchen.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_CITY_MUNCHEN * ratio);
            buttonCityMunchen.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_CITY_MUNCHEN * ratio);

            buttonCityNurenberg.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_CITY_NURENB * ratio);
            buttonCityNurenberg.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_CITY_NURENB * ratio);

            buttonCityPassau.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_CITY_PASSAU * ratio);
            buttonCityPassau.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_CITY_PASSAU * ratio);

            buttonCityPilsen.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_CITY_PILSEN * ratio);
            buttonCityPilsen.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_CITY_PILSEN * ratio);

            buttonCityRegensburg.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_CITY_REGENS * ratio);
            buttonCityRegensburg.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_CITY_REGENS * ratio);

            buttonCitySalzburg.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_CITY_SALZ * ratio);
            buttonCitySalzburg.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_CITY_SALZ * ratio);

            buttonCitySigmaringen.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_CITY_SIGM * ratio);
            buttonCitySigmaringen.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_CITY_SIGM * ratio);

            buttonCityStuttgart.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_CITY_STUTTGART * ratio);
            buttonCityStuttgart.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_CITY_STUTTGART * ratio);

            buttonCityUlm.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_CITY_ULM * ratio);
            buttonCityUlm.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_CITY_ULM * ratio);

            buttonCityWurzburg.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_CITY_WURZ * ratio);
            buttonCityWurzburg.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_CITY_WURZ * ratio);

            buttonCityZurich.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_CITY_ZURICH * ratio);
            buttonCityZurich.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_CITY_ZURICH * ratio);

            wrapHousesAugsburg.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_WRAP_AUGSBURG * ratio);
            wrapHousesAugsburg.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_WRAP_AUGSBURG * ratio);

            wrapHousesBasel.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_WRAP_BASEL * ratio);
            wrapHousesBasel.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_WRAP_BASEL * ratio);

            wrapHousesBudweis.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_WRAP_BUDWEIS * ratio);
            wrapHousesBudweis.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_WRAP_BUDWEIS * ratio);

            wrapHousesCarlsruhe.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_WRAP_CARL * ratio);
            wrapHousesCarlsruhe.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_WRAP_CARL * ratio);

            wrapHousesFreiburg.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_WRAP_FREI * ratio);
            wrapHousesFreiburg.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_WRAP_FREI * ratio);

            wrapHousesIngolstadt.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_WRAP_INGOL * ratio);
            wrapHousesIngolstadt.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_WRAP_INGOL * ratio);

            wrapHousesInnsbruck.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_WRAP_INNS * ratio);
            wrapHousesInnsbruck.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_WRAP_INNS * ratio);

            wrapHousesKempten.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_WRAP_KEMPTEN * ratio);
            wrapHousesKempten.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_WRAP_KEMPTEN * ratio);

            wrapHousesLinz.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_WRAP_LINZ * ratio);
            wrapHousesLinz.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_WRAP_LINZ * ratio);

            wrapHousesLodz.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_WRAP_LODZ * ratio);
            wrapHousesLodz.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_WRAP_LODZ * ratio);

            wrapHousesMannheim.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_WRAP_MANN * ratio);
            wrapHousesMannheim.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_WRAP_MANN * ratio);

            wrapHousesMunchen.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_WRAP_MUNCHEN * ratio);
            wrapHousesMunchen.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_WRAP_MUNCHEN * ratio);

            wrapHousesNurenberg.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_WRAP_NURENB * ratio);
            wrapHousesNurenberg.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_WRAP_NURENB * ratio);

            wrapHousesPassau.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_WRAP_PASSAU * ratio);
            wrapHousesPassau.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_WRAP_PASSAU * ratio);

            wrapHousesPilsen.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_WRAP_PILSEN * ratio);
            wrapHousesPilsen.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_WRAP_PILSEN * ratio);

            wrapHousesRegensburg.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_WRAP_REGENS * ratio);
            wrapHousesRegensburg.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_WRAP_REGENS * ratio);

            wrapHousesSalzburg.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_WRAP_SALZ * ratio);
            wrapHousesSalzburg.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_WRAP_SALZ * ratio);

            wrapHousesSigmaringen.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_WRAP_SIGM * ratio);
            wrapHousesSigmaringen.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_WRAP_SIGM * ratio);

            wrapHousesStuttgart.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_WRAP_STUTTGART * ratio);
            wrapHousesStuttgart.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_WRAP_STUTTGART * ratio);

            wrapHousesUlm.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_WRAP_ULM * ratio);
            wrapHousesUlm.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_WRAP_ULM * ratio);

            wrapHousesWurzburg.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_WRAP_WURZ * ratio);
            wrapHousesWurzburg.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_WRAP_WURZ * ratio);

            wrapHousesZurich.SetValue(Canvas.TopProperty, DEFAULT_DISTANCE_TOP_WRAP_ZURICH * ratio);
            wrapHousesZurich.SetValue(Canvas.LeftProperty, DEFAULT_DISTANCE_LEFT_WRAP_ZURICH * ratio);

        }

        public void ReceivedPlayerList(List<String> players, List<Boolean> states)
        {            
            _playerList = new List<Player>();
            _playerList.Add(null);
            _playerList.Add(null);
            _playerList.Add(null);
            _playerList.Add(null);

            for (int i = 0; i < 4; i++)
            {
                if (players[i] != null)
                {                    
                    Player p = new Player(players[i], GetPlayerColor(i));
                    p.IsReady = states[i];
                    _playerList[i] = p;
                }
            }

            RefreshView();
        }        

        private void RefreshView()
        {
            String color;

            for (int i = 0; i < _playerList.Count; i++)
            {
                if (_playerList[i] != null)
                {
                    int index = GetPlayerDrawIndex(_playerList[i].Nickname);
                    if (index == 0)
                    {
                        labelNickname.Content = _playerList[i].Nickname;
                        Stream imageStreamSource = new FileStream(_playerList[i].CarrigeCard.Source, FileMode.Open, FileAccess.Read, FileShare.Read);
                        GifBitmapDecoder decoder = new GifBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                        BitmapSource bitmapSource = decoder.Frames[0];
                        imageCarrige.Source = bitmapSource;

                        color = _playerList[i].HouseColor;
                        String sourceFile = "";
                        if (color.Equals(House.COLOR_BLUE))
                            sourceFile = "resources/images/casa_blue.png";
                        else if (color.Equals(House.COLOR_RED))
                            sourceFile = "resources/images/casa_red.png";
                        else if (color.Equals(House.COLOR_YELLOW))
                            sourceFile = "resources/images/casa_yellow.png";
                        else if (color.Equals(House.COLOR_GREEN))
                            sourceFile = "resources/images/casa_green.png";

                        imageStreamSource = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read);
                        PngBitmapDecoder decoderD = new PngBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                        bitmapSource = decoderD.Frames[0];

                        imageHouse.Source = bitmapSource;
                    }
                    else if (index == 1)
                    {
                        playerBox1.UpdateImage(_playerList[i].CarrigeCard.Source);
                        playerBox1.UpdateNickname(_playerList[i].Nickname);
                        if (_playerList[i].IsReady)
                            playerBox1.Opacity = 1;
                        else
                            playerBox1.Opacity = 0.2;
                        
                        playerBox1.Visibility = Visibility.Visible;

                        color = _playerList[i].HouseColor;

                        if (color.Equals(House.COLOR_BLUE))
                            playerBox1.UpdateHouse("resources/images/casa_blue.png");
                        else if (color.Equals(House.COLOR_RED))
                            playerBox1.UpdateHouse("resources/images/casa_red.png");
                        else if (color.Equals(House.COLOR_YELLOW))
                            playerBox1.UpdateHouse("resources/images/casa_yellow.png");
                        else if (color.Equals(House.COLOR_GREEN))
                            playerBox1.UpdateHouse("resources/images/casa_green.png");

                    }
                    else if (index == 2)
                    {
                        playerBox2.UpdateImage(_playerList[i].CarrigeCard.Source);
                        playerBox2.UpdateNickname(_playerList[i].Nickname);
                        if (_playerList[i].IsReady)
                            playerBox2.Opacity = 1;
                        else
                            playerBox2.Opacity = 0.2;
                        
                        playerBox2.Visibility = Visibility.Visible;
                        color = _playerList[i].HouseColor;

                        if (color.Equals(House.COLOR_BLUE))
                            playerBox2.UpdateHouse("resources/images/casa_blue.png");
                        else if (color.Equals(House.COLOR_RED))
                            playerBox2.UpdateHouse("resources/images/casa_red.png");
                        else if (color.Equals(House.COLOR_YELLOW))
                            playerBox2.UpdateHouse("resources/images/casa_yellow.png");
                        else if (color.Equals(House.COLOR_GREEN))
                            playerBox2.UpdateHouse("resources/images/casa_green.png");
                    }
                    else if (index == 3)
                    {
                        playerBox3.UpdateImage(_playerList[i].CarrigeCard.Source);
                        playerBox3.UpdateNickname(_playerList[i].Nickname);
                        if (_playerList[i].IsReady)
                            playerBox3.Opacity = 1;
                        else
                            playerBox3.Opacity = 0.2;
                        
                        playerBox3.Visibility = Visibility.Visible;
                        color = _playerList[i].HouseColor;

                        if (color.Equals(House.COLOR_BLUE))
                            playerBox3.UpdateHouse("resources/images/casa_blue.png");
                        else if (color.Equals(House.COLOR_RED))
                            playerBox3.UpdateHouse("resources/images/casa_red.png");
                        else if (color.Equals(House.COLOR_YELLOW))
                            playerBox3.UpdateHouse("resources/images/casa_yellow.png");
                        else if (color.Equals(House.COLOR_GREEN))
                            playerBox3.UpdateHouse("resources/images/casa_green.png");
                    }
                }
            }
        }
              
        private String GetPlayerColor(int position)
        {
            switch (position)
            {
                case 0:
                    return House.COLOR_BLUE;
                    
                case 1:
                    return House.COLOR_RED;
                    
                case 2:
                    return House.COLOR_GREEN;
                    
                case 3:
                    return House.COLOR_YELLOW;
                    
            }

            return "NaN";
        }

        private Player GetPlayer(String nickname)
        {            
            for (int i = 0; i < _playerList.Count; i++)
                if (_playerList[i] != null)
                    if (_playerList[i].Nickname.Equals(nickname))
                        return _playerList[i];
            return null;
        }

        private Int32 GetPlayerRealIndex(String nickname)
        {
            for (int i = 0; i < _playerList.Count; i++)
                if (_playerList[i] != null) 
                    if (_playerList[i].Nickname.Equals(nickname))
                        return i;
            return -1;
        }

        private Int32 GetPlayerDrawIndex(String nickname)
        {
            int userPostion = -1;
            int playerPosition = -1;

            for (int i = 0; i < _playerList.Count; i++)
                if (_playerList[i] != null)
                {
                    if (_playerList[i].Nickname.Equals(nickname))
                        playerPosition = i;                        
                    if (_playerList[i].Nickname.Equals(_gameUI.GetGameSettings().Nickname))
                        userPostion = i;
                }
            if ((userPostion != -1) && (playerPosition != -1))
                return (playerPosition + 4 - userPostion ) % 4; ;
            return -1;
        }

        private void buttonReady_Click(object sender, RoutedEventArgs e)
        {
            Player p = GetPlayer(_gameUI.GetGameSettings().Nickname);
            if (p != null)
            {
                if (_gameUI != null)
                {
                    Boolean _isReady = p.IsReady;
                    if (_isReady)
                    {
                        p.IsReady = false;
                        _gameUI.SetState(false, _roomId);
                        buttonReady.Content = "I'm Ready";
                    }
                    else
                    {
                        p.IsReady = true;
                        _gameUI.SetState(true, _roomId);
                        buttonReady.Content = "I'm Not Ready";
                    }
                }
            }
        }

        private void UpdateStartButton()
        {
            Boolean _isStartAvailable = true;

            Player pp = GetPlayer(_gameUI.GetGameSettings().Nickname);
            for (int i = 0; i < _playerList.Count; i++)
            {
                if (_playerList[i] != null)
                    if (_playerList[i] != pp)
                        if (_playerList[i].IsReady == false)
                        {
                            _isStartAvailable = false;
                            break;
                        }
            }

            if (_isStartAvailable)
                buttonReady.IsEnabled = true;
            else
                buttonReady.IsEnabled = false;
        }

        private void Update6Cards(List<CityCard> cityCards) 
        {
            if (cityCards.Count != 6)
                return;

            String sourceFile;

            if (cityCards[0] != null)
            {
                sourceFile = CityCard.GetCityCardImageSource(cityCards[0].CityName);
                Stream imageStreamSource = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read);
                PngBitmapDecoder decoder = new PngBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                BitmapSource bitmapSource = decoder.Frames[0];

                if (imageCard0 != null)
                {
                    imageCard0.Source = bitmapSource;
                    buttonCard0.Visibility = Visibility.Visible;
                }
            }

            if (cityCards[1] != null)
            {
                sourceFile = CityCard.GetCityCardImageSource(cityCards[1].CityName);
                Stream imageStreamSource = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read);
                PngBitmapDecoder decoder = new PngBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                BitmapSource bitmapSource = decoder.Frames[0];

                if (imageCard1 != null)
                {
                    imageCard1.Source = bitmapSource;
                    buttonCard1.Visibility = Visibility.Visible;
                }
            }

            if (cityCards[2] != null)
            {
                sourceFile = CityCard.GetCityCardImageSource(cityCards[2].CityName);
                Stream imageStreamSource = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read);
                PngBitmapDecoder decoder = new PngBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                BitmapSource bitmapSource = decoder.Frames[0];

                if (imageCard2 != null)
                {
                    imageCard2.Source = bitmapSource;
                    buttonCard2.Visibility = Visibility.Visible;
                }
            }

            if (cityCards[3] != null)
            {
                sourceFile = CityCard.GetCityCardImageSource(cityCards[3].CityName);
                Stream imageStreamSource = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read);
                PngBitmapDecoder decoder = new PngBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                BitmapSource bitmapSource = decoder.Frames[0];

                if (imageCard3 != null)
                {
                    imageCard3.Source = bitmapSource;
                    buttonCard3.Visibility = Visibility.Visible;
                }
            }

            if (cityCards[4] != null)
            {
                sourceFile = CityCard.GetCityCardImageSource(cityCards[4].CityName);
                Stream imageStreamSource = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read);
                PngBitmapDecoder decoder = new PngBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                BitmapSource bitmapSource = decoder.Frames[0];

                if (imageCard4 != null)
                {
                    imageCard4.Source = bitmapSource;
                    buttonCard4.Visibility = Visibility.Visible;
                }
            }

            if (cityCards[5] != null)
            {
                sourceFile = CityCard.GetCityCardImageSource(cityCards[5].CityName);
                Stream imageStreamSource = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read);
                PngBitmapDecoder decoder = new PngBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                BitmapSource bitmapSource = decoder.Frames[0];

                if (imageCard5 != null)
                {
                    imageCard5.Source = bitmapSource;
                    buttonCard5.Visibility = Visibility.Visible;
                }
            }

        }

        private void UpdateCard(Int32 position, String cityName)
        {
            String sourceFile;
            Stream imageStreamSource;
            PngBitmapDecoder decoder;
            BitmapSource bitmapSource;

            switch (position)
            {
                case 0:
                    sourceFile = CityCard.GetCityCardImageSource(cityName);
                    imageStreamSource = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read);
                    decoder = new PngBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                    bitmapSource = decoder.Frames[0];
                    if (imageCard0 != null)
                    {
                        imageCard0.Source = bitmapSource;
                        buttonCard0.Visibility = Visibility.Visible;
                    }
                    break;
                case 1:
                    sourceFile = CityCard.GetCityCardImageSource(cityName);
                    imageStreamSource = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read);
                    decoder = new PngBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                    bitmapSource = decoder.Frames[0];
                    if (imageCard1 != null)
                    {
                        imageCard1.Source = bitmapSource;
                        buttonCard1.Visibility = Visibility.Visible;
                    }
                    break;
                case 2:
                    sourceFile = CityCard.GetCityCardImageSource(cityName);
                    imageStreamSource = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read);
                    decoder = new PngBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                    bitmapSource = decoder.Frames[0];
                    if (imageCard2 != null)
                    {
                        imageCard2.Source = bitmapSource;
                        buttonCard2.Visibility = Visibility.Visible;
                    }
                    break;
                case 3:
                    sourceFile = CityCard.GetCityCardImageSource(cityName);
                    imageStreamSource = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read);
                    decoder = new PngBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                    bitmapSource = decoder.Frames[0];
                    if (imageCard3 != null)
                    {
                        imageCard3.Source = bitmapSource;
                        buttonCard3.Visibility = Visibility.Visible;
                    }
                    break;
                case 4:
                    sourceFile = CityCard.GetCityCardImageSource(cityName);
                    imageStreamSource = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read);
                    decoder = new PngBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                    bitmapSource = decoder.Frames[0];
                    if (imageCard4 != null)
                    {
                        imageCard4.Source = bitmapSource;
                        buttonCard4.Visibility = Visibility.Visible;
                    }
                    break;
                case 5:
                    sourceFile = CityCard.GetCityCardImageSource(cityName);
                    imageStreamSource = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read);
                    decoder = new PngBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                    bitmapSource = decoder.Frames[0];
                    if (imageCard5 != null)
                    {
                        imageCard5.Source = bitmapSource;
                        buttonCard5.Visibility = Visibility.Visible;
                    }
                    break;               

            }
        }

        private void RefreshMoveOptions(ArrayList moveList)
        {
            ResetAllMoves();

            for (int i = 0; i < moveList.Count; i++)
            {
                int move = (int)moveList[i];
                switch (move)
                {
                    case MoveConstants.CLOSE_AND_SCORE_ROUTE:
                        buttonScore.Visibility = Visibility.Visible;
                        break;
                    case MoveConstants.DISCARD_CARDS:
                        break;
                    case MoveConstants.END_TURN:
                        buttonEndTurn.Visibility = Visibility.Visible;
                        break;                    
                    case MoveConstants.PLACE_HOUSES:
                        break;
                    case MoveConstants.PUT_CARD:
                        _statePrivateButtons = STATE_PUT_DOWN;
                        for (int j = 0; j < _privateCardButtons.Count; j++ )
                            _privateCardButtons[j].IsEnabled = true;
                        if (_publicCardButtons.Count != 0)
                            buttonDiscardCards.Visibility = Visibility.Visible;
                        break;
                    case MoveConstants.TAKE_CARD:
                        buttonCard0.IsEnabled = true;
                        buttonCard1.IsEnabled = true;
                        buttonCard2.IsEnabled = true;
                        buttonCard3.IsEnabled = true;
                        buttonCard4.IsEnabled = true;
                        buttonCard5.IsEnabled = true;
                        buttonDrawBackCard.IsEnabled = true;
                        break;
                    case MoveConstants.USE_ADMINISTRATOR:
                        imageAdministrator.Visibility = Visibility.Visible;
                        buttonAdministrator.Visibility = Visibility.Visible;
                        break;
                    case MoveConstants.USE_CARTWRIGHT:
                        imageCartWright.Visibility = Visibility.Visible;
                        buttonCartwright.Visibility = Visibility.Visible;
                        break;
                    case MoveConstants.USE_POSTAL_CARRIER:
                        imagePostalCarrier.Visibility = Visibility.Visible;
                        break;
                    case MoveConstants.USE_POSTMASTER:
                        imagePostMaster.Visibility = Visibility.Visible;
                        break;
                }
            }
            
        }

        private void ResetAllMoves()
        {
            buttonCityAugsburg.Visibility = Visibility.Hidden;
            buttonCityBasel.Visibility = Visibility.Hidden;
            buttonCityBudweis.Visibility = Visibility.Hidden;
            buttonCityCarlsruhe.Visibility = Visibility.Hidden;
            buttonCityFreiburg.Visibility = Visibility.Hidden;
            buttonCityIngolstadt.Visibility = Visibility.Hidden;
            buttonCityInnsbruck.Visibility = Visibility.Hidden;
            buttonCityKempten.Visibility = Visibility.Hidden;
            buttonCityLinz.Visibility = Visibility.Hidden;
            buttonCityLodz.Visibility = Visibility.Hidden;
            buttonCityMannheim.Visibility = Visibility.Hidden;
            buttonCityMunchen.Visibility = Visibility.Hidden;
            buttonCityNurenberg.Visibility = Visibility.Hidden;
            buttonCityPassau.Visibility = Visibility.Hidden;
            buttonCityPilsen.Visibility = Visibility.Hidden;
            buttonCityRegensburg.Visibility = Visibility.Hidden;
            buttonCitySalzburg.Visibility = Visibility.Hidden;
            buttonCitySigmaringen.Visibility = Visibility.Hidden;
            buttonCityStuttgart.Visibility = Visibility.Hidden;
            buttonCityUlm.Visibility = Visibility.Hidden;
            buttonCityWurzburg.Visibility = Visibility.Hidden;
            buttonCityZurich.Visibility = Visibility.Hidden;

            imageMap.Visibility = Visibility.Visible;
            buttonCard0.Visibility = Visibility.Visible;
            buttonCard1.Visibility = Visibility.Visible;
            buttonCard2.Visibility = Visibility.Visible;
            buttonCard3.Visibility = Visibility.Visible;
            buttonCard4.Visibility = Visibility.Visible;
            buttonCard5.Visibility = Visibility.Visible;

            imageBonusAllRegions.Visibility = Visibility.Visible;
            imageBonusBaden.Visibility = Visibility.Visible;
            imageBonusBaiern.Visibility = Visibility.Visible;
            imageBonusCarrige5.Visibility = Visibility.Visible;
            imageBonusCarrige6.Visibility = Visibility.Visible;
            imageBonusCarrige7.Visibility = Visibility.Visible;
            imageBonusFinish.Visibility = Visibility.Visible;
            imageBonusSalzburg.Visibility = Visibility.Visible;
            imageBonusTyrol.Visibility = Visibility.Visible;
            imageBonusWurtt.Visibility = Visibility.Visible;

            imageCarrige3.Visibility = Visibility.Visible;
            imageCarrige4.Visibility = Visibility.Visible;
            imageCarrige5.Visibility = Visibility.Visible;
            imageCarrige6.Visibility = Visibility.Visible;
            imageCarrige7.Visibility = Visibility.Visible;

            buttonCard0.IsEnabled = false;
            buttonCard1.IsEnabled = false;
            buttonCard2.IsEnabled = false;
            buttonCard3.IsEnabled = false;
            buttonCard4.IsEnabled = false;
            buttonCard5.IsEnabled = false;
            buttonDrawBackCard.IsEnabled = false;

            imagePostMaster.Visibility = Visibility.Hidden;
            imagePostalCarrier.Visibility = Visibility.Hidden;
            imageAdministrator.Visibility = Visibility.Hidden;
            imageCartWright.Visibility = Visibility.Hidden;

            buttonCartwright.Visibility = Visibility.Hidden;
            buttonScore.Visibility = Visibility.Hidden;
            buttonEndTurn.Visibility = Visibility.Hidden;
            buttonAdministrator.Visibility = Visibility.Hidden;
            buttonFinish.Visibility = Visibility.Hidden;
            buttonDiscardCards.Visibility = Visibility.Hidden;

            for (int i = 0; i < _privateCardButtons.Count; i++)
                _privateCardButtons[i].IsEnabled = false;
        }

        private void buttonCard0_Click(object sender, RoutedEventArgs e)
        {
            if (_gameUI != null)
            {
                soundCardChange.Stop();
                soundCardChange.Play();

                CityCard currentCard = _gameUI.GetDisplayCard(0);
                Button imgButton = CreateImageCardButton(currentCard);
                _privateCardButtons.Add(imgButton);
                wrapPrivateCards.Children.Add(imgButton);

                CityCard newCard = _gameUI.IDrawCard(0, _roomId);
                UpdateCard(0, newCard.CityName);

                RefreshMoveOptions(_gameUI.GetAvailableMoves(_gameUI.GetGameSettings().Nickname));
                
            }
        }

        private void buttonCard1_Click(object sender, RoutedEventArgs e)
        {
            if (_gameUI != null)
            {
                soundCardChange.Stop();
                soundCardChange.Play();
                CityCard currentCard = _gameUI.GetDisplayCard(1);
                Button imgButton = CreateImageCardButton(currentCard);
                _privateCardButtons.Add(imgButton);
                wrapPrivateCards.Children.Add(imgButton);

                CityCard newCard = _gameUI.IDrawCard(1, _roomId);
                UpdateCard(1, newCard.CityName);

                RefreshMoveOptions(_gameUI.GetAvailableMoves(_gameUI.GetGameSettings().Nickname));

            }
        }

        private void buttonCard2_Click(object sender, RoutedEventArgs e)
        {
            if (_gameUI != null)
            {
                soundCardChange.Stop();
                soundCardChange.Play();
                CityCard currentCard = _gameUI.GetDisplayCard(2);
                Button imgButton = CreateImageCardButton(currentCard);
                _privateCardButtons.Add(imgButton);
                wrapPrivateCards.Children.Add(imgButton);

                CityCard newCard = _gameUI.IDrawCard(2, _roomId);
                UpdateCard(2, newCard.CityName);

                RefreshMoveOptions(_gameUI.GetAvailableMoves(_gameUI.GetGameSettings().Nickname));

            }
        }

        private void buttonCard3_Click(object sender, RoutedEventArgs e)
        {
            if (_gameUI != null)
            {
                soundCardChange.Stop();
                soundCardChange.Play();
                CityCard currentCard = _gameUI.GetDisplayCard(3);
                Button imgButton = CreateImageCardButton(currentCard);
                _privateCardButtons.Add(imgButton);
                wrapPrivateCards.Children.Add(imgButton);

                CityCard newCard = _gameUI.IDrawCard(3, _roomId);
                UpdateCard(3, newCard.CityName);

                RefreshMoveOptions(_gameUI.GetAvailableMoves(_gameUI.GetGameSettings().Nickname));

            }
        }

        private void buttonCard4_Click(object sender, RoutedEventArgs e)
        {
            if (_gameUI != null)
            {
                soundCardChange.Stop();
                soundCardChange.Play();
                CityCard currentCard = _gameUI.GetDisplayCard(4);
                Button imgButton = CreateImageCardButton(currentCard);
                _privateCardButtons.Add(imgButton);
                wrapPrivateCards.Children.Add(imgButton);

                CityCard newCard = _gameUI.IDrawCard(4, _roomId);
                UpdateCard(4, newCard.CityName);

                RefreshMoveOptions(_gameUI.GetAvailableMoves(_gameUI.GetGameSettings().Nickname));

            }
        }

        private void buttonCard5_Click(object sender, RoutedEventArgs e)
        {
            if (_gameUI != null)
            {
                soundCardChange.Stop();
                soundCardChange.Play();
                CityCard currentCard = _gameUI.GetDisplayCard(5);
                Button imgButton = CreateImageCardButton(currentCard);
                _privateCardButtons.Add(imgButton);
                wrapPrivateCards.Children.Add(imgButton);

                CityCard newCard = _gameUI.IDrawCard(5, _roomId);
                UpdateCard(5, newCard.CityName);

                RefreshMoveOptions(_gameUI.GetAvailableMoves(_gameUI.GetGameSettings().Nickname));

            }
        }

        private void buttonDrawBackCard_Click(object sender, RoutedEventArgs e)
        {
            if (_gameUI != null)
            {
                soundCardChange.Stop();
                soundCardChange.Play();
                CityCard c = _gameUI.IDrawCard(-1, _roomId);
                Button imgButton = CreateImageCardButton(c);
                _privateCardButtons.Add(imgButton);
                wrapPrivateCards.Children.Add(imgButton);
                RefreshMoveOptions(_gameUI.GetAvailableMoves(_gameUI.GetGameSettings().Nickname));

            }
        }

        private Button CreateLeftRightButton(CityCard c)
        {
            Button b = new Button();
            Image img = new Image();

            String sourceFile = CityCard.GetCityCardImageSource(c.CityName);
            Stream imageStreamSource = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read);
            PngBitmapDecoder decoder = new PngBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
            BitmapSource bitmapSource = decoder.Frames[0];

            img.Source = bitmapSource;
            b.Content = img;

            return b;
        }

        private Button CreateImageCardButton(CityCard c)
        {
            Button b = new Button();
            Image img = new Image();

            String sourceFile = CityCard.GetCityCardImageSource(c.CityName);
            Stream imageStreamSource = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read);
            PngBitmapDecoder decoder = new PngBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
            BitmapSource bitmapSource = decoder.Frames[0];

            if (img != null)
                img.Source = bitmapSource;
            else
                return null;

            b.IsEnabled = false;
            b.Content = img;
            b.Tag = c;
            b.Click += new RoutedEventHandler(buttonPrivateCard_Click);

            return b;
            
        }

        void buttonPrivateCard_Click(object sender, RoutedEventArgs e)
        {
            Button b = (Button)sender;

            if (_statePrivateButtons == STATE_PUT_DOWN)
            {
                if (_privateButtonPressed != null)
                {
                    if (_stateIndermediatePrivateButtons == MoveConstants.POSITION_NONE)
                    {
                        //TODO
                    }
                    else if (_stateIndermediatePrivateButtons == MoveConstants.POSITION_BOTH)
                    {
                        wrapPublicCards.Children.RemoveAt(0);
                        wrapPublicCards.Children.RemoveAt(wrapPublicCards.Children.Count - 1);
                    }
                    else if (_stateIndermediatePrivateButtons == MoveConstants.POSITION_LEFT_ONLY)
                        wrapPublicCards.Children.RemoveAt(0);
                    else if (_stateIndermediatePrivateButtons == MoveConstants.POSITION_RIGHT_ONLY)
                        wrapPublicCards.Children.RemoveAt(wrapPublicCards.Children.Count - 1);



                }
                if (_privateButtonPressed != b)
                {
                    CityCard c = (CityCard)b.Tag;

                    int position = _gameUI.GetCardPutPosition(c);

                    if (position == MoveConstants.POSITION_NONE)
                    {
                        _stateIndermediatePrivateButtons = MoveConstants.POSITION_NONE;
                        _privateButtonPressed = b;
                    }
                    else if (position == MoveConstants.POSITION_LEFT_ONLY)
                    {
                        buttonLeft = CreateLeftRightButton(c); //new Button();
                        buttonLeft.IsEnabled = true;
                        buttonLeft.Opacity = 0.5;
                        
                        //buttonLeft.Content = "left";
                        buttonLeft.Tag = sender;
                        buttonLeft.Click += new RoutedEventHandler(buttonLeft_Click);
                        wrapPublicCards.Children.Insert(0, buttonLeft);

                        _privateButtonPressed = b;
                        _stateIndermediatePrivateButtons = MoveConstants.POSITION_LEFT_ONLY;
                    }
                    else if (position == MoveConstants.POSITION_RIGHT_ONLY)
                    {
                        buttonRight = CreateLeftRightButton(c); // new Button();
                        buttonRight.IsEnabled = true;
                        buttonRight.Opacity = 0.5;
                        //buttonRight.Content = "right";
                        buttonRight.Tag = sender;
                        buttonRight.Click += new RoutedEventHandler(buttonRight_Click);
                        wrapPublicCards.Children.Add(buttonRight);

                        _privateButtonPressed = b;
                        _stateIndermediatePrivateButtons = MoveConstants.POSITION_RIGHT_ONLY;
                    }
                    else if (position == MoveConstants.POSITION_BOTH)
                    {
                        buttonLeft = CreateLeftRightButton(c); //new Button();
                        buttonLeft.IsEnabled = true;
                        buttonLeft.Opacity = 0.5;
                        //buttonLeft.Content = "left";
                        buttonLeft.Tag = sender;
                        buttonLeft.Click += new RoutedEventHandler(buttonLeft_Click);
                        wrapPublicCards.Children.Insert(0, buttonLeft);

                        buttonRight = CreateLeftRightButton(c); //new Button();
                        buttonRight.IsEnabled = true;
                        buttonRight.Opacity = 0.5;
                        //buttonRight.Content = "right";
                        buttonRight.Tag = sender;
                        buttonRight.Click += new RoutedEventHandler(buttonRight_Click);
                        wrapPublicCards.Children.Add(buttonRight);

                        _privateButtonPressed = b;
                        _stateIndermediatePrivateButtons = MoveConstants.POSITION_BOTH;
                    }
                }
                else
                    _privateButtonPressed = null;
            }
            else
            {
                int index = _temporaryButtons.IndexOf(b);
                if (index == -1)
                {
                    if ((_privateCardButtons.Count - _temporaryButtons.Count) > 3)
                    {
                        _temporaryButtons.Add(b);
                        b.Opacity = 0.5;
                    }
                }
                else
                {
                    _temporaryButtons.RemoveAt(index);
                    b.Opacity = 1;
                }
            }
        }

        void buttonRight_Click(object sender, RoutedEventArgs e)
        {
            soundCardChange.Stop();
            soundCardChange.Play();
            Button b = (Button)((Button)sender).Tag;


            if (_stateIndermediatePrivateButtons == MoveConstants.POSITION_BOTH)
            {
                wrapPublicCards.Children.RemoveAt(0);
                wrapPublicCards.Children.RemoveAt(wrapPublicCards.Children.Count - 1);
            }
            else if (_stateIndermediatePrivateButtons == MoveConstants.POSITION_LEFT_ONLY)
                wrapPublicCards.Children.RemoveAt(0);
            else if (_stateIndermediatePrivateButtons == MoveConstants.POSITION_RIGHT_ONLY)
                wrapPublicCards.Children.RemoveAt(wrapPublicCards.Children.Count - 1);

            wrapPrivateCards.Children.Remove(b);
            _privateCardButtons.Remove(b);

            b.IsEnabled = false;
            wrapPublicCards.Children.Add(b);
            _publicCardButtons.Add(b);

            _stateIndermediatePrivateButtons = -1;
            _gameUI.IPutCard((CityCard)b.Tag, 1, _roomId);
            RefreshMoveOptions(_gameUI.GetAvailableMoves(_gameUI.GetGameSettings().Nickname));

            _privateButtonPressed = null;
            
        }

        void buttonLeft_Click(object sender, RoutedEventArgs e)
        {
            soundCardChange.Stop();
            soundCardChange.Play();
            Button b = (Button)((Button)sender).Tag;

            if (_stateIndermediatePrivateButtons == MoveConstants.POSITION_BOTH)
            {
                wrapPublicCards.Children.RemoveAt(0);
                wrapPublicCards.Children.RemoveAt(wrapPublicCards.Children.Count - 1);
            }
            else if (_stateIndermediatePrivateButtons == MoveConstants.POSITION_LEFT_ONLY)
                wrapPublicCards.Children.RemoveAt(0);
            else if (_stateIndermediatePrivateButtons == MoveConstants.POSITION_RIGHT_ONLY)
                wrapPublicCards.Children.RemoveAt(wrapPublicCards.Children.Count - 1);
            
            wrapPrivateCards.Children.Remove(b);
            _privateCardButtons.Remove(b);

            b.IsEnabled = false;
            wrapPublicCards.Children.Insert(0, b);
            _publicCardButtons.Insert(0, b);

            _stateIndermediatePrivateButtons = -1;
            _gameUI.IPutCard((CityCard)b.Tag, 0, _roomId);
            RefreshMoveOptions(_gameUI.GetAvailableMoves(_gameUI.GetGameSettings().Nickname));

            _privateButtonPressed = null;
        }

        private void buttonEndTurn_Click(object sender, RoutedEventArgs e)
        {
            if (buttonLeft != null)
                buttonLeft.Visibility = Visibility.Collapsed;
            if (buttonRight != null)
                buttonRight.Visibility = Visibility.Collapsed;
            if (_gameUI != null)
            {
                _gameUI.IEndTurn(_roomId);
                ResetAllMoves();
            }
        }

        private void buttonAdministrator_Click(object sender, RoutedEventArgs e)
        {
            if (_gameUI != null)
            {
                soundCardChange.Stop();
                soundCardChange.Play();
                List<CityCard> list =  _gameUI.IUseAdministrator(_roomId);
                Update6Cards(list);

                RefreshMoveOptions(_gameUI.GetAvailableMoves(_gameUI.GetGameSettings().Nickname));
            }
                
        }

        private void buttonScore_Click(object sender, RoutedEventArgs e)
        {
            if (buttonLeft != null) 
                buttonLeft.Visibility = Visibility.Collapsed;
            if (buttonRight != null)
                buttonRight.Visibility = Visibility.Collapsed;

            /// Get Road Length Bonuses ///
            if (_publicCardButtons.Count > 4)
            {
                BonusCard bCard = _gameUI.CalculateBonusRoadLength(_gameUI.GetGameSettings().Nickname);
                if (bCard != null)
                    UpdateBonuses(_gameUI.GetGameSettings().Nickname, bCard);
            }
            /////////////////////////////////
            CarrigeCard cCard = _gameUI.CalculateCarrigeCard(_gameUI.GetGameSettings().Nickname, false);
            if (cCard != null)
                UpdateCarrigeCard(_gameUI.GetGameSettings().Nickname, cCard);


            wrapPublicCards.Children.Clear();
            _publicCardButtons.Clear();
            buttonEndTurn.Visibility = Visibility.Hidden;
            List<String> cityNames = _gameUI.GetCitiesForHouses(_gameUI.GetGameSettings().Nickname);
            if (cityNames.Count > 0)
            {
                buttonScore.Visibility = Visibility.Hidden;
                buttonFinish.Visibility = Visibility.Visible;
                UpdateLayout4Houses(cityNames);
                _temporaryHouses = new List<String>();
                _temporaryImageHouses = new List<Image>();
            }

            //todo else
        }

        private void buttonFinish_Click(object sender, RoutedEventArgs e)
        {
            _gameUI.IPutHouses(_temporaryHouses);

            labelHousesCount.Content = Convert.ToInt32(labelHousesCount.Content) - _temporaryHouses.Count;

            BonusCard bCard;

            bCard = _gameUI.CalculateBonusAllRegion(_gameUI.GetGameSettings().Nickname);
            if (bCard != null)
                UpdateBonuses(_gameUI.GetGameSettings().Nickname, bCard);

            bCard = _gameUI.CalculateBonusRegion(_gameUI.GetGameSettings().Nickname, BonusCard.BONUS_REGION_BADEN);
            if (bCard != null)
                UpdateBonuses(_gameUI.GetGameSettings().Nickname, bCard);

            bCard = _gameUI.CalculateBonusRegion(_gameUI.GetGameSettings().Nickname, BonusCard.BONUS_REGION_BAIERN);
            if (bCard != null)
                UpdateBonuses(_gameUI.GetGameSettings().Nickname, bCard);

            bCard = _gameUI.CalculateBonusRegion(_gameUI.GetGameSettings().Nickname, BonusCard.BONUS_REGION_BOHEM_SALZB);
            if (bCard != null)
                UpdateBonuses(_gameUI.GetGameSettings().Nickname, bCard);

            bCard = _gameUI.CalculateBonusRegion(_gameUI.GetGameSettings().Nickname, BonusCard.BONUS_REGION_SCHWEIZ_TYROL);
            if (bCard != null)
                UpdateBonuses(_gameUI.GetGameSettings().Nickname, bCard);

            bCard = _gameUI.CalculateBonusRegion(_gameUI.GetGameSettings().Nickname, BonusCard.BONUS_REGION_WURTT_HOHENZ);
            if (bCard != null)
                UpdateBonuses(_gameUI.GetGameSettings().Nickname, bCard);

            bCard = _gameUI.CalculateEndGame(_gameUI.GetGameSettings().Nickname);
            if (bCard != null)
            {
                UpdateBonuses(_gameUI.GetGameSettings().Nickname, bCard);
                _gameUI.IScore();
                EndGame();
                return;
            }

            ResetAllMoves();

            if (_privateCardButtons.Count > 3)
            {
                _statePrivateButtons = STATE_DISCARD;
                buttonDiscardCards.Visibility = Visibility.Visible;
                _temporaryButtons = new List<Button>();
                for (int i = 0; i < _privateCardButtons.Count; i++)
                    _privateCardButtons[i].IsEnabled = true;
            }
            else
            {
                _gameUI.IScore();
            }
        }
        
        #region Put Houses Functions 

        
        private void buttonCityHouse_Click(object sender, RoutedEventArgs e)
        {
            String cityName = (String)((Button)sender).Tag;
            List<String> cityNames;

            for (int i = 0; i < _temporaryHouses.Count; i++)
            {
                if (_temporaryHouses[i].Equals(cityName))
                {
                    _temporaryHouses.RemoveAt(i);                    
                    RemoveHouseImageInCity(cityName, _temporaryImageHouses[i]);
                    _temporaryImageHouses.RemoveAt(i);
                    cityNames = _gameUI.GetCitiesForHouses(_gameUI.GetGameSettings().Nickname, _temporaryHouses);
                    UpdateLayout4Houses(cityNames);
                    return;
                }
            }

            String color = _gameUI.GetPlayerColor(_gameUI.GetGameSettings().Nickname);
            Image img = DrawHouseInCity(color, cityName);
            _temporaryHouses.Add(cityName);
            _temporaryImageHouses.Add(img);

            cityNames = _gameUI.GetCitiesForHouses(_gameUI.GetGameSettings().Nickname, _temporaryHouses);
            UpdateLayout4Houses(cityNames);
            
        }
        
        private void UpdateLayout4Houses(List<String> cityNames)
        {
            buttonCityAugsburg.Visibility = Visibility.Hidden;
            buttonCityBasel.Visibility = Visibility.Hidden;
            buttonCityBudweis.Visibility = Visibility.Hidden;
            buttonCityCarlsruhe.Visibility = Visibility.Hidden;
            buttonCityFreiburg.Visibility = Visibility.Hidden;
            buttonCityIngolstadt.Visibility = Visibility.Hidden;
            buttonCityInnsbruck.Visibility = Visibility.Hidden;
            buttonCityKempten.Visibility = Visibility.Hidden;
            buttonCityLinz.Visibility = Visibility.Hidden;
            buttonCityLodz.Visibility = Visibility.Hidden;
            buttonCityMannheim.Visibility = Visibility.Hidden;
            buttonCityMunchen.Visibility = Visibility.Hidden;
            buttonCityNurenberg.Visibility = Visibility.Hidden;
            buttonCityPassau.Visibility = Visibility.Hidden;
            buttonCityPilsen.Visibility = Visibility.Hidden;
            buttonCityRegensburg.Visibility = Visibility.Hidden;
            buttonCitySalzburg.Visibility = Visibility.Hidden;
            buttonCitySigmaringen.Visibility = Visibility.Hidden;
            buttonCityStuttgart.Visibility = Visibility.Hidden;
            buttonCityUlm.Visibility = Visibility.Hidden;
            buttonCityWurzburg.Visibility = Visibility.Hidden;
            buttonCityZurich.Visibility = Visibility.Hidden;
            
            for (int i = 0; i < cityNames.Count; i++)
            {
                if (cityNames[i].Equals(City.AUGSBURG))
                    buttonCityAugsburg.Visibility = Visibility.Visible;                
                else if (cityNames[i].Equals(City.BASEL))
                    buttonCityBasel.Visibility = Visibility.Visible;
                if (cityNames[i].Equals(City.BUDWEIS))
                    buttonCityBudweis.Visibility = Visibility.Visible;
                else if (cityNames[i].Equals(City.CARLSRUHE))
                    buttonCityCarlsruhe.Visibility = Visibility.Visible;
                if (cityNames[i].Equals(City.FREIBURG))
                    buttonCityFreiburg.Visibility = Visibility.Visible;
                else if (cityNames[i].Equals(City.INGOLSTADT))
                    buttonCityIngolstadt.Visibility = Visibility.Visible;
                if (cityNames[i].Equals(City.INNSBRUCK))
                    buttonCityInnsbruck.Visibility = Visibility.Visible;
                else if (cityNames[i].Equals(City.KEMPTEN))
                    buttonCityKempten.Visibility = Visibility.Visible;
                if (cityNames[i].Equals(City.LINZ))
                    buttonCityLinz.Visibility = Visibility.Visible;
                else if (cityNames[i].Equals(City.LODZ))
                    buttonCityLodz.Visibility = Visibility.Visible;
                if (cityNames[i].Equals(City.MANNHEIM))
                    buttonCityMannheim.Visibility = Visibility.Visible;
                else if (cityNames[i].Equals(City.MUNCHEN))
                    buttonCityMunchen.Visibility = Visibility.Visible;
                if (cityNames[i].Equals(City.NURENBERG))
                    buttonCityNurenberg.Visibility = Visibility.Visible;
                else if (cityNames[i].Equals(City.PASSAU))
                    buttonCityPassau.Visibility = Visibility.Visible;
                if (cityNames[i].Equals(City.PILSEN))
                    buttonCityPilsen.Visibility = Visibility.Visible;
                else if (cityNames[i].Equals(City.REGENSBURG))
                    buttonCityRegensburg.Visibility = Visibility.Visible;
                if (cityNames[i].Equals(City.SALZBURG))
                    buttonCitySalzburg.Visibility = Visibility.Visible;
                else if (cityNames[i].Equals(City.SIGMARINGEN))
                    buttonCitySigmaringen.Visibility = Visibility.Visible;
                if (cityNames[i].Equals(City.STUTTGART))
                    buttonCityStuttgart.Visibility = Visibility.Visible;
                else if (cityNames[i].Equals(City.ULM))
                    buttonCityUlm.Visibility = Visibility.Visible;
                if (cityNames[i].Equals(City.WURZBURG))
                    buttonCityWurzburg.Visibility = Visibility.Visible;
                else if (cityNames[i].Equals(City.ZURICH))
                    buttonCityZurich.Visibility = Visibility.Visible;
            }
        }

        private Image GetHouseImageAndPosition(String color)
        {
            String sourceFile = "";
            if (color.Equals(House.COLOR_BLUE))
                sourceFile = "resources/images/casa_blue.png";
            else if (color.Equals(House.COLOR_RED))
                sourceFile = "resources/images/casa_red.png";
            else if (color.Equals(House.COLOR_YELLOW))
                sourceFile = "resources/images/casa_yellow.png";
            else if (color.Equals(House.COLOR_GREEN))
                sourceFile = "resources/images/casa_green.png";

            Stream imageStreamSource = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read);
            PngBitmapDecoder decoder = new PngBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
            BitmapSource bitmapSource = decoder.Frames[0];

            Image img = new Image();
            img.Source = bitmapSource;


            return img;
        }

        private Image DrawHouseInCity(String color, String cityName)
        {
            Image img = GetHouseImageAndPosition(color);

            if (cityName.Equals(City.AUGSBURG))
                wrapHousesAugsburg.Children.Add(img);
            else if (cityName.Equals(City.BASEL))
                wrapHousesBasel.Children.Add(img);
            else if (cityName.Equals(City.BUDWEIS))
                wrapHousesBudweis.Children.Add(img);
            else if (cityName.Equals(City.CARLSRUHE))
                wrapHousesCarlsruhe.Children.Add(img);
            else if (cityName.Equals(City.FREIBURG))
                wrapHousesFreiburg.Children.Add(img);
            else if (cityName.Equals(City.INGOLSTADT))
                wrapHousesIngolstadt.Children.Add(img);
            else if (cityName.Equals(City.INNSBRUCK))
                wrapHousesInnsbruck.Children.Add(img);
            else if (cityName.Equals(City.KEMPTEN))
                wrapHousesKempten.Children.Add(img);
            else if (cityName.Equals(City.LINZ))
                wrapHousesLinz.Children.Add(img);
            else if (cityName.Equals(City.LODZ))
                wrapHousesLodz.Children.Add(img);
            else if (cityName.Equals(City.MANNHEIM))
                wrapHousesMannheim.Children.Add(img);
            else if (cityName.Equals(City.MUNCHEN))
                wrapHousesMunchen.Children.Add(img);
            else if (cityName.Equals(City.NURENBERG))
                wrapHousesNurenberg.Children.Add(img);
            else if (cityName.Equals(City.PASSAU))
                wrapHousesPassau.Children.Add(img);
            else if (cityName.Equals(City.PILSEN))
                wrapHousesPilsen.Children.Add(img);
            else if (cityName.Equals(City.REGENSBURG))
                wrapHousesRegensburg.Children.Add(img);
            else if (cityName.Equals(City.SALZBURG))
                wrapHousesSalzburg.Children.Add(img);
            else if (cityName.Equals(City.SIGMARINGEN))
                wrapHousesSigmaringen.Children.Add(img);
            else if (cityName.Equals(City.STUTTGART))
                wrapHousesStuttgart.Children.Add(img);
            else if (cityName.Equals(City.ULM))
                wrapHousesUlm.Children.Add(img);
            else if (cityName.Equals(City.WURZBURG))
                wrapHousesWurzburg.Children.Add(img);
            else if (cityName.Equals(City.ZURICH))
                wrapHousesZurich.Children.Add(img);

            return img;
        }

        private Image GetBonusImage(BonusCard card)
        {
            String sourceFile = card.Source;

            Stream imageStreamSource = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read);
            GifBitmapDecoder decoder = new GifBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
            BitmapSource bitmapSource = decoder.Frames[0];

            Image img = new Image();
            img.Height = DEFAULT_HEIGHT_BONUS * System.Windows.SystemParameters.PrimaryScreenHeight / DEFAULT_HEIGHT;
            img.Width = DEFAULT_HEIGHT_BONUS * System.Windows.SystemParameters.PrimaryScreenHeight / DEFAULT_HEIGHT;
            
            img.Source = bitmapSource;

            return img;
        }

        private void RemoveHouseImageInCity(String cityName, Image img)
        {

            if (cityName.Equals(City.AUGSBURG))
                wrapHousesAugsburg.Children.Remove(img);
            else if (cityName.Equals(City.BASEL))
                wrapHousesBasel.Children.Remove(img);
            else if (cityName.Equals(City.BUDWEIS))
                wrapHousesBudweis.Children.Remove(img);
            else if (cityName.Equals(City.CARLSRUHE))
                wrapHousesCarlsruhe.Children.Remove(img);
            else if (cityName.Equals(City.FREIBURG))
                wrapHousesFreiburg.Children.Remove(img);
            else if (cityName.Equals(City.INGOLSTADT))
                wrapHousesIngolstadt.Children.Remove(img);
            else if (cityName.Equals(City.INNSBRUCK))
                wrapHousesInnsbruck.Children.Remove(img);
            else if (cityName.Equals(City.KEMPTEN))
                wrapHousesKempten.Children.Remove(img);
            else if (cityName.Equals(City.LINZ))
                wrapHousesLinz.Children.Remove(img);
            else if (cityName.Equals(City.LODZ))
                wrapHousesLodz.Children.Remove(img);
            else if (cityName.Equals(City.MANNHEIM))
                wrapHousesMannheim.Children.Remove(img);
            else if (cityName.Equals(City.MUNCHEN))
                wrapHousesMunchen.Children.Remove(img);
            else if (cityName.Equals(City.NURENBERG))
                wrapHousesNurenberg.Children.Remove(img);
            else if (cityName.Equals(City.PASSAU))
                wrapHousesPassau.Children.Remove(img);
            else if (cityName.Equals(City.PILSEN))
                wrapHousesPilsen.Children.Remove(img);
            else if (cityName.Equals(City.REGENSBURG))
                wrapHousesRegensburg.Children.Remove(img);
            else if (cityName.Equals(City.SALZBURG))
                wrapHousesSalzburg.Children.Remove(img);
            else if (cityName.Equals(City.SIGMARINGEN))
                wrapHousesSigmaringen.Children.Remove(img);
            else if (cityName.Equals(City.STUTTGART))
                wrapHousesStuttgart.Children.Remove(img);
            else if (cityName.Equals(City.ULM))
                wrapHousesUlm.Children.Remove(img);
            else if (cityName.Equals(City.WURZBURG))
                wrapHousesWurzburg.Children.Remove(img);
            else if (cityName.Equals(City.ZURICH))
                wrapHousesZurich.Children.Remove(img);


            

        }

        #endregion

        private void buttonDiscardCards_Click(object sender, RoutedEventArgs e)
        {
            soundCardChange.Stop();
            soundCardChange.Play();
            if (_statePrivateButtons == STATE_PUT_DOWN)
            {
                wrapPublicCards.Children.Clear();
                _publicCardButtons.Clear();
                _gameUI.IDiscardPublicCards();
            }
            else
            {
                List<CityCard> cardsList = new List<CityCard>();

                for (int i = 0; i < _temporaryButtons.Count; i++)
                {
                    cardsList.Add((CityCard)_temporaryButtons[i].Tag);                    
                    _privateCardButtons.Remove(_temporaryButtons[i]);
                    wrapPrivateCards.Children.Remove(_temporaryButtons[i]);                    
                }
                _temporaryButtons.Clear();
                _gameUI.IDiscardPrivateCards(cardsList);
                if (_privateCardButtons.Count < 4)
                {
                    _gameUI.IScore();
                    ResetAllMoves();
                }
            }
        }

        private void buttonQuit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void UpdateCarrigeCard(String nickname, CarrigeCard card)
        {
            int index = GetPlayerDrawIndex(nickname);
            Stream imageStreamSource;
            GifBitmapDecoder decoder;
            BitmapSource bitmapSource;
            CarrigeCard cc;
            
            if (index == 0) {
                imageStreamSource = new FileStream(card.Source, FileMode.Open, FileAccess.Read, FileShare.Read);
                decoder = new GifBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                bitmapSource = decoder.Frames[0];
                imageCarrige.Source = bitmapSource;
            }
            else if (index == 1)
                playerBox1.UpdateImage(card.Source);
            else if (index == 2)
                playerBox2.UpdateImage(card.Source);
            else if (index == 3)
                playerBox3.UpdateImage(card.Source);

            
            

            switch (card.Value)
            {
                case 3:
                    cc = _gameUI.GetCurrentCarrigeCard(3);
                    if (cc == null)
                        imageCarrige3.Source = null;
                    else
                    {
                        imageStreamSource = new FileStream(cc.Source, FileMode.Open, FileAccess.Read, FileShare.Read);
                        decoder = new GifBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                        bitmapSource = decoder.Frames[0];
                        imageCarrige3.Source = bitmapSource;
                    }
                    break;
                case 4:
                    cc = _gameUI.GetCurrentCarrigeCard(4);
                    if (cc == null)
                        imageCarrige4.Source = null;
                    else
                    {
                        imageStreamSource = new FileStream(cc.Source, FileMode.Open, FileAccess.Read, FileShare.Read);
                        decoder = new GifBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                        bitmapSource = decoder.Frames[0];
                        imageCarrige4.Source = bitmapSource;
                    }
                    break;
                case 5:
                    cc = _gameUI.GetCurrentCarrigeCard(5);
                    if (cc == null)
                        imageCarrige5.Source = null;
                    else
                    {
                        imageStreamSource = new FileStream(cc.Source, FileMode.Open, FileAccess.Read, FileShare.Read);
                        decoder = new GifBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                        bitmapSource = decoder.Frames[0];
                        imageCarrige5.Source = bitmapSource;
                    }
                    break;
                case 6:
                    cc = _gameUI.GetCurrentCarrigeCard(6);
                    if (cc == null)
                        imageCarrige6.Source = null;
                    else
                    {
                        imageStreamSource = new FileStream(cc.Source, FileMode.Open, FileAccess.Read, FileShare.Read);
                        decoder = new GifBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                        bitmapSource = decoder.Frames[0];
                        imageCarrige6.Source = bitmapSource;
                    }
                    break;
                case 7:
                    cc = _gameUI.GetCurrentCarrigeCard(7);
                    if (cc == null)
                        imageCarrige7.Source = null;
                    else
                    {
                        imageStreamSource = new FileStream(cc.Source, FileMode.Open, FileAccess.Read, FileShare.Read);
                        decoder = new GifBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                        bitmapSource = decoder.Frames[0];
                        imageCarrige7.Source = bitmapSource;
                    }
                    break;
            }

        }

        private void UpdateBonuses(String nickname, BonusCard bonusCard)
        {
            int index = GetPlayerDrawIndex(nickname);

            if (index == 0)
            {
                Image img = GetBonusImage(bonusCard);
                wrapPanelBonuses.Children.Add(img);
            }
            else if (index == 1)
                playerBox1.UpdateBonuses(bonusCard);
            else if (index == 2)
                playerBox2.UpdateBonuses(bonusCard);
            else if (index == 3)
                playerBox3.UpdateBonuses(bonusCard);

            BonusCard bb;
            Stream imageStreamSource;
            GifBitmapDecoder decoder;
            BitmapSource bitmapSource;

            switch (bonusCard.Type)
            {
                case BonusCard.BONUS_FINISH_GAME:
                    imageBonusFinish.Source = null;                    
                    break;
                case BonusCard.BONUS_REGION_ALL:
                    bb = _gameUI.GetCurrentBonus(BonusCard.BONUS_REGION_ALL);
                    if (bb == null)
                        imageBonusAllRegions.Source = null;
                    else
                    {
                        imageStreamSource = new FileStream(bb.Source, FileMode.Open, FileAccess.Read, FileShare.Read);
                        decoder = new GifBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                        bitmapSource = decoder.Frames[0];
                        imageBonusAllRegions.Source = bitmapSource;
                    }
                    break;
                case BonusCard.BONUS_REGION_BADEN:
                    bb = _gameUI.GetCurrentBonus(BonusCard.BONUS_REGION_BADEN);
                    if (bb == null)
                        imageBonusBaden.Source = null;
                    else
                    {
                        imageStreamSource = new FileStream(bb.Source, FileMode.Open, FileAccess.Read, FileShare.Read);
                        decoder = new GifBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                        bitmapSource = decoder.Frames[0];
                        imageBonusBaden.Source = bitmapSource;
                    }
                    break;
                case BonusCard.BONUS_REGION_BAIERN:
                    bb = _gameUI.GetCurrentBonus(BonusCard.BONUS_REGION_BAIERN);
                    if (bb == null)
                        imageBonusBaiern.Source = null;
                    else
                    {
                        imageStreamSource = new FileStream(bb.Source, FileMode.Open, FileAccess.Read, FileShare.Read);
                        decoder = new GifBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                        bitmapSource = decoder.Frames[0];
                        imageBonusBaiern.Source = bitmapSource;
                    }
                    break;
                case BonusCard.BONUS_REGION_BOHEM_SALZB:
                    bb = _gameUI.GetCurrentBonus(BonusCard.BONUS_REGION_BOHEM_SALZB);
                    if (bb == null)
                        imageBonusSalzburg.Source = null;
                    else
                    {
                        imageStreamSource = new FileStream(bb.Source, FileMode.Open, FileAccess.Read, FileShare.Read);
                        decoder = new GifBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                        bitmapSource = decoder.Frames[0];
                        imageBonusSalzburg.Source = bitmapSource;
                    }
                    break;
                case BonusCard.BONUS_REGION_SCHWEIZ_TYROL:
                    bb = _gameUI.GetCurrentBonus(BonusCard.BONUS_REGION_SCHWEIZ_TYROL);
                    if (bb == null)
                        imageBonusTyrol.Source = null;
                    else
                    {
                        imageStreamSource = new FileStream(bb.Source, FileMode.Open, FileAccess.Read, FileShare.Read);
                        decoder = new GifBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                        bitmapSource = decoder.Frames[0];
                        imageBonusTyrol.Source = bitmapSource;
                    }
                    break;
                case BonusCard.BONUS_REGION_WURTT_HOHENZ:
                    bb = _gameUI.GetCurrentBonus(BonusCard.BONUS_REGION_WURTT_HOHENZ);
                    if (bb == null)
                        imageBonusWurtt.Source = null;
                    else
                    {
                        imageStreamSource = new FileStream(bb.Source, FileMode.Open, FileAccess.Read, FileShare.Read);
                        decoder = new GifBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                        bitmapSource = decoder.Frames[0];
                        imageBonusWurtt.Source = bitmapSource;
                    }
                    break;
                case BonusCard.BONUS_ROAD_LENGTH_5:
                    bb = _gameUI.GetCurrentBonus(BonusCard.BONUS_ROAD_LENGTH_5);
                    if (bb == null)
                        imageBonusCarrige5.Source = null;
                    else
                    {
                        imageStreamSource = new FileStream(bb.Source, FileMode.Open, FileAccess.Read, FileShare.Read);
                        decoder = new GifBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                        bitmapSource = decoder.Frames[0];
                        imageBonusCarrige5.Source = bitmapSource;
                    }
                    break;
                case BonusCard.BONUS_ROAD_LENGTH_6:
                    bb = _gameUI.GetCurrentBonus(BonusCard.BONUS_ROAD_LENGTH_6);
                    if (bb == null)
                        imageBonusCarrige6.Source = null;
                    else
                    {
                        imageStreamSource = new FileStream(bb.Source, FileMode.Open, FileAccess.Read, FileShare.Read);
                        decoder = new GifBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                        bitmapSource = decoder.Frames[0];
                        imageBonusCarrige6.Source = bitmapSource;
                    }
                    break;
                case BonusCard.BONUS_ROAD_LENGTH_7:
                    bb = _gameUI.GetCurrentBonus(BonusCard.BONUS_ROAD_LENGTH_7);
                    if (bb == null)
                        imageBonusCarrige7.Source = null;
                    else
                    {
                        imageStreamSource = new FileStream(bb.Source, FileMode.Open, FileAccess.Read, FileShare.Read);
                        decoder = new GifBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                        bitmapSource = decoder.Frames[0];
                        imageBonusCarrige7.Source = bitmapSource;
                    }
                    break;
            }
        }

        private void window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (_gameUI != null)
                _gameUI.QuitGame(_roomId);
        }

        private void EndGame()
        {
            //gridGame.Visibility = Visibility.Collapsed;
            gridGame.Children.Clear();
            components.EndGameBox box = new Thurn_und_Taxis.components.EndGameBox();
            gridGame.Children.Add(box);
            box.UpdateScores(_gameUI.GetPlayers());
        }

        private void buttonCartwright_Click(object sender, RoutedEventArgs e)
        {
            _gameUI.IScoreHelp();
            /// Get Road Length Bonuses ///
            if (_publicCardButtons.Count > 4)
            {
                BonusCard bCard = _gameUI.CalculateBonusRoadLength(_gameUI.GetGameSettings().Nickname);
                if (bCard != null)
                    UpdateBonuses(_gameUI.GetGameSettings().Nickname, bCard);
            }
            /////////////////////////////////
            CarrigeCard cCard = _gameUI.CalculateCarrigeCard(_gameUI.GetGameSettings().Nickname, true);
            if (cCard != null)
                UpdateCarrigeCard(_gameUI.GetGameSettings().Nickname, cCard);


            wrapPublicCards.Children.Clear();
            _publicCardButtons.Clear();
            buttonEndTurn.Visibility = Visibility.Hidden;
            buttonCartwright.Visibility = Visibility.Hidden;

            List<String> cityNames = _gameUI.GetCitiesForHouses(_gameUI.GetGameSettings().Nickname);
            if (cityNames.Count > 0)
            {
                buttonScore.Visibility = Visibility.Hidden;
                buttonFinish.Visibility = Visibility.Visible;
                UpdateLayout4Houses(cityNames);
                _temporaryHouses = new List<String>();
                _temporaryImageHouses = new List<Image>();
            }
        }


    }
}
